我有一个这样的 NSString:
NSString *word = @"119,111,114,100"
所以,我想做的是把这个 NSString 转换成 word
所以问题是,我可以通过哪种方式将字符串转换为单词?
// I have added some values to your sample input :-)
NSString *word = @"119,111,114,100,32,240,159,145,141";
// Separate components into array:
NSArray *array = [word componentsSeparatedByString:@","];
// Create NSData containing the bytes:
NSMutableData *data = [[NSMutableData alloc] initWithLength:[array count]];
uint8_t *bytes = [data mutableBytes];
for (NSUInteger i = 0; i < [array count]; i++) {
bytes[i] = [array[i] intValue];
}
// Convert to NSString (interpreting the bytes as UTF-8):
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
输出:
单词
尝试这个:
NSString *word = @"119,111,114,100";
NSArray *array=[word componentsSeparatedByString:@","];
for (NSString *string in array) {
char character=[string integerValue];
NSLog(@"%c",character);
}
输出:
w
o
r
d
libicu它是一个 UTF8 库,支持从此处所述的字节数组进行转换。
问题是,它提供 Java、C 或 C++ API,而不是 obj-c。