[英]How convert string utf-8?
我有這樣的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);
輸出:
word 👍
嘗試這個:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.