NSNonLossyASCIIStringEncoding returns nil

泄露秘密 提交于 2019-12-10 01:58:44

问题


I'm working on default emojis in iOS. i'm able to successfully encode and decode default emojis using NSNonLossyASCIIStringEncoding encoding.

Its working fine when i sent emojis with simple text but it returns nil when some special character is added in string. How do i make it work ?

Code :

    testString=":;Hello \ud83d\ude09\ud83d\ude00 ., <> /?\";
    NSData *data = [testString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *strBody = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding]; 
    // here strBody is nil

回答1:


The problem is due to different encodings you have used for encoding and decoding.

 testString=":;Hello \ud83d\ude09\ud83d\ude00 ., <> /?\";
 NSData *data = [testString dataUsingEncoding:NSUTF8StringEncoding];

Here you have converted a string to data using UTF8 encoding. This means it will convert the unicode characters in 1-4 bytes depending on the unicode character used. for e.g. \ude09 will translate to ED B8 89. The explanation of the same is available in wiki. Basically is uses the following technique:

Now if you try to decode this to string using ascii encoding like below

   NSString *strBody = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding]; 

The above is bound to fail as it cannot decode ED B8 89 or similar unicode data to ascii string. That's why it returns an error.

If the data was ascii encoded, it would have used literal ascii hex for the conversion. So \ude09 would have become "5c 75 64 65 30 39"

So the correct conversion would be :

    testString=":;Hello \ud83d\ude09\ud83d\ude00 ., <> /?\";
    NSData *data = [testString dataUsingEncoding:NSNonLossyASCIIStringEncoding];
    NSString *strBody = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding]; 

The question for you is why you want it to encode as UTF8 and decode as ASCII?


For emojis, please try the below

        testString=":;Hello \\ud83d\\ude09\\ud83d\\ude00 ., <> /?";
        NSData *data = [testString dataUsingEncoding:NSUTF8StringEncoding];
        NSString *strBody = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding]; 



回答2:


If you simply want to have emojis in your code as literals, there are two options:

A. Just do it:

NSString *hello = @"😀😎+_)(&#&)#&)$&$)&$)^#%!!#$%!";
NSLog(@"%@", hello);

B. Add the codes as UTF32

NSString *hello = @"\U0001F600\U0001F60E+_)(&#&)#&)$&$)&$)^#%!!#$%!";
NSLog(@"%@", hello);

Both prints: 😀😎+_)(&#&)#&)$&$)&$)^#%!!#$%!

I really do not get your problem.



来源:https://stackoverflow.com/questions/42021291/nsnonlossyasciistringencoding-returns-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!