简体   繁体   中英

NSData to NSString encoding

I have an application that receives messages from server. Those messages may contain cyrillic characters. But when I transform received data into NSString I obtain only "\М\а\к" symbols instead of cyrrilic ones.

   NSData *responceData = ....;

   NSString* responceString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

How may I get correct symbols?

There's a much easier solution.

If your data has literal unicode escape sequences in it (that is, \М\\0430\\043a as pure ASCII characters, with no unicode escaping applied), then this is not the UTF-8 encoding of that string. You want NSNonLossyASCIIStringEncoding .

NSData *responseData = ....;

NSString* responseString = [[NSString alloc] initWithData:responseData encoding:NSNonLossyASCIIStringEncoding];

responseString will now be exactly what you expect.

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