NSNonLossyASCIIStringEncoding equivalent for Android

前端 未结 3 715
梦毁少年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 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.

提交回复
热议问题