我在iOS中有一个字符串变量,我想将其转换为字符数组,然后转换为十六进制字节,如0xD6,0xD6等。
如果Objective-C中有一个我可以用于此
的库,那将会很棒答案 0 :(得分:2)
swift 4
string to byte:
let strChar = "A"
let data1 = [UInt8](self.strChar.utf8)
答案 1 :(得分:1)
可能会在这里回答:
字符串to chars:
NSString *s = @"Some string";
const char *c = [s UTF8String];
chars to hex:
- (NSData *)dataFromHexString {
const char *chars = [self UTF8String];
int i = 0, len = self.length;
NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
char byteChars[3] = {'\0','\0','\0'};
unsigned long wholeByte;
while (i < len) {
byteChars[0] = chars[i++];
byteChars[1] = chars[i++];
wholeByte = strtoul(byteChars, NULL, 16);
[data appendBytes:&wholeByte length:1];
}
return data;
}