29,028
社区成员
发帖
与我相关
我的任务
分享
//传入一个16进制字符串:@"328A6102033B516386668561DF5F0B7E"
-(void)getByteWithString:(NSString *)string
{
NSMutableArray *strMutArr = [[NSMutableArray alloc]init];
for (int i = 0; i < string.length; i += 2)
{
NSString *str = [NSString stringWithFormat:@"0x%@",[string substringWithRange:NSMakeRange(i, 2)]];
[strMutArr bd_addObject:str];
}
//strMutArr打印结果:Printing description of strMutArr:<__NSArrayM 0x145db8730>(0x32,0x8A,0x61,0x02,0x03, 0x3B,0x51,0x63,0x86,0x66,0x85,0x61,0xDF,0x5F,0x0B,0x7E)
Byte bytes[16];
for (int j = 0; j < strMutArr.count; j++)
{
const char *c = [strMutArr[j] UTF8String];
Byte byte = (Byte)strtol(c, NULL, 16);
bytes[j] = byte;
}
//bytes结果:(Byte [16]) bytes = {
// [0] = '2'
// [1] = '\x8a'
// [2] = 'a'
// [3] = '\x02'
// [4] = '\x03'
// [5] = ';'
// [6] = 'Q'
// [7] = 'c'
// [8] = '\x86'
// [9] = 'f'
// [10] = '\x85'
// [11] = 'a'
// [12] = '\xdf'
// [13] = '_'
// [14] = '\v'
// [15] = '~'
//}
}