爲十六進制字符串轉換爲數據,看到我張貼在這裏的功能: Converting a hexadecimal string into binary in Objective-C
您將要稍微修改它允許括號字符和空格。
要轉到另一個方向:
NSString *CreateHexStringWithData(NSData *data)
{
NSUInteger inLength = [data length];
unichar *outCharacters = malloc(sizeof(unichar) * (inLength * 2));
UInt8 *inBytes = (UInt8 *)[data bytes];
static const char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
NSUInteger i, o = 0;
for (i = 0; i < inLength; i++) {
UInt8 inByte = inBytes[i];
outCharacters[o++] = lookup[(inByte & 0xF0) >> 4];
outCharacters[o++] = lookup[(inByte & 0x0F)];
}
return [[NSString alloc] initWithCharactersNoCopy:outCharacters length:o freeWhenDone:YES];
}
您還可以使用:
[NSString stringWithFormat:@"%@", data]
使用 - [NSData的描述]獲得與括號和空格的版本。
我用這個和iccir的解決方案的組合。我需要十六進制字符串來使用這個解決方案!謝謝! – evdude100