I'm trying to translate this snippet :
ntohs(*(UInt16*)VALUE) / 4.0
and some other ones, looking alike, from C to Swift. Problem is, I have very few knowledge of Swift and I just can't understand what this snippet does... Here's all I know :
ntohsswap endianness to host endiannessVALUEis achar[32]- I just discovered that Swift :
(UInt(data.0) << 6) + (UInt(data.1) >> 2)does the same thing. Could one please explain ? - I'm willing to return a Swift
Uint(UInt64)
Thanks !
VALUEis a pointer to 32 bytes (char[32]).UInt16pointer. That means the first two bytes ofVALUEare being interpreted asUInt16(2 bytes).*will dereference the pointer. We get the two bytes ofVALUEas a 16-bit number. However it has net endianness (net byte order), so we cannot make integer operations on it.4.0.To do the same in Swift, let's just compose the byte values to an integer.
Note that to divide by
4.0you will have to convert the integer toFloat.