It appears there are tons of posts about how to put values into NSData but I'm looking to to do the opposite here. I'm trying to parse some data that will be arriving over a socket and wanted to migrate some old obj-c code over to swift. In obj-c I'm able to do
const char *bytes = [message bytes];
NSInteger targetAddress = (((unsigned char) bytes[2]) << 16)
| ((unsigned char) bytes[3] << 8)
| (unsigned char) bytes[4];
int status = ((unsigned char) bytes[1] & 0xF0) >> 4;
I have run into trouble with how to correctly migrate this to swift.
I'm able to convert the following into a NSData array in swift for testing but can't figure out how to get it back out:
var testBytes : [Byte] = [0x14, 0x00, 0xAB, 0x45, 0x49,
0x1F, 0xEF, 0x15, 0xA8, 0x89, 0x78, 0x0F, 0x09,
0xA9, 0x07, 0xB0, 0x01, 0x20, 0x01, 0x4E, 0x38,
0x32, 0x35, 0x56, 0x20, 0x20, 0x20, 0x00]
var msgData = NSMutable(bytes: testBytes, length: testBytes.count)
The .bytes
call in swift returns an UnsafePointer<void>
so I'm not sure exactly what call to make.
Thanks