Objective c 如何在Plist中存储十六进制值并再次读取
如果我一直想弄明白这一点,现在我的头脑变得一团糟 我想做的是在plist中的NSData中存储一个十六进制值。 然后我想能够读回十六进制 我感到非常困惑 所以我尝试存储十六进制值Objective c 如何在Plist中存储十六进制值并再次读取,objective-c,hex,plist,Objective C,Hex,Plist,如果我一直想弄明白这一点,现在我的头脑变得一团糟 我想做的是在plist中的NSData中存储一个十六进制值。 然后我想能够读回十六进制 我感到非常困惑 所以我尝试存储十六进制值0x1124。 当我查看生成的plist时,值显示为2411000。 当我打印这个值时,我得到23FA0 我希望能够做的是确认0x1124已写入我的plist,并确保可以打印回正确的值 我觉得我缺少一些基本的东西 NSMutableDictionary *tempDict=[NSMutableDictionary ne
0x1124
。
当我查看生成的plist时,值显示为2411000
。
当我打印这个值时,我得到23FA0
我希望能够做的是确认0x1124已写入我的plist,并确保可以打印回正确的值
我觉得我缺少一些基本的东西
NSMutableDictionary *tempDict=[NSMutableDictionary new];
// Byte hidService= 1124;
//int hidService= 00001124-0000-1000-8000-00805f9b34fb
unsigned int hidService[]={0x1124};
NSData *classlist=[NSData dataWithBytes:&hidService length:sizeof(hidService)];
NSArray *classListArray=@[classlist];
[tempDict setValue:classListArray forKey:kServiceItemKeyServiceClassIDList];
hidProfileDict=[[NSDictionary alloc]initWithDictionary:tempDict];
NSLog(@"%X",[hidProfileDict valueForKey:kServiceItemKeyServiceClassIDList][0]);
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager=[NSFileManager defaultManager];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"HIDDictionary.plist"];
if (![fileManager fileExistsAtPath: plistPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"HIDDictionary" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:plistPath error:&error];
}
[hidProfileDict writeToFile:plistPath atomically: YES];
如果只想存储两个字节,请使用显式UInt16类型
UInt16 hidService[]={0x1124};
NSData *classlist = [NSData dataWithBytes:&hidService length:sizeof(hidService)];
0x1124
只是二进制位000100100100100100或十进制数4388的十六进制表示。0x只是一种指定显示底部的方法,它不是数字的一部分。该数字可以在二进制程序中用0b前缀表示:int b=0b000100100代码>。这些都只是同一个数字的不同表示
要将数字添加到NSDictionary
或NSArray
中,需要将其转换为NSNumber
,最简单的方法是使用文字语法:@(0x1124)
或@(4388)
例:
或
我只想在plist中存储一个十六进制值,这样我就可以作为参数值加载回接受十六进制值的IOBluetooth。在我的例子中,函数期望的十六进制值是0x1124。您需要研究一个数字成为十六进制值意味着什么。提示:十六进制只是数字的显示格式,与十六进制:1124、二进制:000100100100100或十进制:4388的显示格式相同。他们都是同一个号码。
NSArray *a = @[@(0x1124)];
NSDictionary *d = @{kServiceItemKeyServiceClassIDList:@(0x1124)};
// Where kServiceItemKeyServiceClassIDList is defined to be a `NSString`.