简体   繁体   English

如何转换字符串utf-8?

[英]How convert string utf-8?

i've an NSString like this: 我有这样的NSString:

NSString *word = @"119,111,114,100"

So, what i want to do is to convert this NSString to word 所以,我要做的就是将此NSString转换为word

So the question is, in which way can i convert a string to a 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);

Output: 输出:

word 👍

Try this: 尝试这个:

NSString *word = @"119,111,114,100";
NSArray *array=[word componentsSeparatedByString:@","];
for (NSString *string in array) {
   char character=[string integerValue];
    NSLog(@"%c",character);    
}

Output: 输出:

w
o
r
d

libicu it's an UTF8 library that supports a conversion from an array of bytes as stated here . libicu它支持从字节数组转换为规定的UTF8库这里

The thing is, it offers Java, C or C++ APIs, not obj-c. 事实是,它提供了Java,C或C ++ API,而不是obj-c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM