Shuvo Joseph Shuvo Joseph - 3 months ago 26
iOS Question

Convert Hex NSData to int from Smart Router response Objective C

Smart Router is giving me NSData response


<800c01ff 0001ffff ffff29ff>


Here,
800c
is my deviceId. How to convert it to int ?


deviceId =32780;


When I send command to router I constructed it like,

Byte mqttData[12];

mqttData[0] =[[NSNumber numberWithUnsignedChar:(deviceID >> 8) & 0xFF] intValue];
//mqttData[0]=128 or 80 in Hex

mqttData[1] =[[NSNumber numberWithUnsignedChar:deviceID & 0xFF] intValue];
//mqttData[1]=12 or 'C' in Hex

return [NSData dataWithBytes:mqttData length:sizeof(mqttData)];

Answer
int deviceId = CFSwapInt16BigToHost(*(int*)([deviceIDData bytes]))

Answer taken from Larme's comment.