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 "\u041c\u0430\u043a" symbols instead of cyrrilic ones.

   NSData *responceData = ....;

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

How may I get correct symbols?

Answers


There's a much easier solution.

If your data has literal unicode escape sequences in it (that is, \u041c\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.


Need Your Help

Singleton class across the whole project approach

c++ multithreading qt oop singleton

I have a singleton class for logging purpose in my Qt project. In each class except the singleton one, there is a pointer point to the singleton object and a signal connected to an writing slot in ...

Android Checking if a file size has exceeded a certain value

java android

Is it possible to set a listener on a created file and alert when the file size has reached a certain value.