2016-06-21 65 views
0
詮釋

我們的自定義智能路由器是給我NSData響應十六進制轉換NSData的在目標C

< 800c01ff 0001ffff ffff29ff>

這裏,800c是我的DeviceID。如何將其轉換爲int

deviceId = 32780;

當我發送命令給路由器構建我喜歡它,從Larme的評論採取

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)]; 
+1

使用'NSData * deviceIDData = [theDataResponse subdataWithRange:NSMakeRange(0,2)],int deviceId; [deviceIDData getBytes:&deviceId length:sizeof(int)];'? – Larme

+1

爲什麼'NSNumber'涉及到所有? – Droppy

+0

@Lamre DEVICEID:5508224.但它是32780 –

回答

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

答案。