NSNonLossyASCIIStringEncoding equivalent for Android

前端 未结 3 716
梦毁少年i
梦毁少年i 2021-01-06 17:23

I got to port some chat code from iOS to Android. Before sending the chat message to the socket, the iOS code uses the NSNonLossyASCIIStringEncoding class as pa

相关标签:
3条回答
  • 2021-01-06 17:49

    Don't use NSNonLossyASCIIStringEncoding, use utf-8 encoding. I just solved this problem myself on ios+android+java spring backend, and it took me around 4 full days to figure everything out. Android can't display emojis, but this gives me full character support in almost all (or all not sure) languages. Here are the articles that helped me:

    Must Read: http://blog.manbolo.com/2012/10/29/supporting-new-emojis-on-ios-6 http://blog.manbolo.com/2011/12/12/supporting-ios-5-new-emoji-encoding

    See the hex bytes of a string inside the DB: How can I see raw bytes stored in a MySQL column?

    Details about how to setup MySQL: http://technovergence-en.blogspot.com/2012/03/mysql-from-utf8-to-utf8mb4.html

    In depth FAQ of utf8- http://www.unicode.org/faq/utf_bom.html#utf8-4

    Details about the difference from notation: \ud83d\udc7d and hex value in memory: 0xF09F91BD http://en.wikipedia.org/wiki/UTF-8#Description

    Use this to copy and paste characters in to see real hex byte values (works for emojis): http://perishablepress.com/tools/utf8-hex/index.php

    Get Spring to support utf8 in urls (for GET params) http://forum.springsource.org/showthread.php?93728-RequestParam-doesn-t-seem-to-be-decoded Get Parameter Encoding http://forum.springsource.org/showthread.php?112181-Unable-to-Override-the-Spring-MVC-URL-decoding-which-uses-default-quot-ISO-8859-1-quot

    0 讨论(0)
  • 2021-01-06 17:58

    My answer code is equivalent to IOS NSNonLossyASCIIStringEncoding for Android.

    In your gradle put below depandancy.

     compile 'org.apache.commons:commons-lang3:3.4'
    

    then Put method to your Utils Class Like this

     public static String encode(String s)
    {
        return StringEscapeUtils.escapeJava(s);
    
    }
    
    public static String decode(String s)
    {
        return StringEscapeUtils.unescapeJava(s);
    
    }
    

    then Simply call this method where you want to encode string or decode String like this

    //for encode
    String stencode = Utils.encode("mystring");
    
    
    //for decode
    String stdecode = Utils.decode("mystring")
    
    0 讨论(0)
  • 2021-01-06 18:08

    @portforwardpodcast is absolutely correct that you should, if possible, avoid ASCII encoding your utf8 and instead set up your stack to handle/store utf8 directly. That said, if you don't have the ability to change the behavior, the following code may be helpful.

    While there's no published explanation of how NSNonLossyASCIIStringEncoding works, based on its output it looks like:

    • Bytes in the extended ASCII range (decimal values 128 - 255) are escaped using an octal encoding (e.g. ñ with decimal value 241 -> \361)
    • Non-ASCII code points are escaped in two byte chunks using a hex encoding (e.g.
    0 讨论(0)
提交回复
热议问题