我有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
答案 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
}