简体   繁体   中英

How to convert NSData to NSString in Japanese

When I convert NSString to NSData, after convert from NSData to NSString

NSString *input = @"ステータス";
NSData *output = [NSData dataWithBytes:[input UTF8String] length:[input length]];
NSString *output2 = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];

The output2 is nil.
If input is alphabet character, it ok.

The correct conversion is

NSData* output = [input dataUsingEncoding:NSUTF8StringEncoding];

The problem with your code is that [input length] gives the number of Unicode characters, not the number of UTF-8 bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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