With Swift I want to convert bytes from a uint8_t array to an integer.
\"C\" Example:
char bytes[2] = {0x01, 0x02};
NSData *data = [NSData dataWithBy
If the bytes are in an NSData
object you may do (assume data:NSData
):
var number: UInt16 = 0
data.getBytes(&number, length: sizeof(UInt16))
The getBytes
method writes up to two bytes in the memory location of number
(similar to C's memcpy
.
This won't crash your app if data
hasn't enough bytes.
(edit: no need to use range if starting from beginning of buffer)