如何从Objective C中的NSData Hex值获取索引的Bit值

时间:2016-06-26 04:36:03

标签: objective-c hex byte nsdata bit

我有NSData Hex

  

< 80140142 0073ffff ffff04ff>

我可以通过索引获取Byte

  

NSData * commandIDData = [mqttResponseData subdataWithRange:NSMakeRange(3,1)];   < 42> // 66

如何通过索引获取Bit值?

  

0100 0010

e.g

  

指数[0] = 0   指数[1] = 1
  指数[2] = 0
  指数[4] = 0

1 个答案:

答案 0 :(得分:2)

NSData *statusData = [mqttResponseData subdataWithRange:NSMakeRange(3,1)];

const unsigned char *statusByte = [statusData bytes];//Hex<4F>/Decimal-79/Binary-01001111

for (int i = 0 ; i < 8; i++) {
    bool chanelValue = [[NSNumber numberWithChar:(*statusByte >> i) & 0x01] boolValue];//i = 0 right most bit
    NSLog(@"chanelValue %d: %d",i+1,chanelValue);//1 1 1 1 0 0 1 0
}