Shuvo Joseph
Shuvo Joseph

Reputation: 912

Convert Hex NSData to int in Objective C

Our custom 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)];

Upvotes: 0

Views: 209

Answers (1)

Shuvo Joseph
Shuvo Joseph

Reputation: 912

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

Answer taken from Larme's comment.

Upvotes: 1

Related Questions