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 :
ntohs
swap endianness to host endiannessVALUE
is 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 !
VALUE
is a pointer to 32 bytes (char[32]
).UInt16
pointer. That means the first two bytes ofVALUE
are being interpreted asUInt16
(2 bytes).*
will dereference the pointer. We get the two bytes ofVALUE
as 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.0
you will have to convert the integer toFloat
.