2

NSNonLossyASCIIStringEncoding equivalent for Android

 2 years ago
source link: https://www.codesd.com/item/nsnonlossyasciistringencoding-equivalent-for-android.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

NSNonLossyASCIIStringEncoding equivalent for Android

advertisements

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 parameter of the NSString::dataUsingEncoding.

How would you do it in Android? Same question about the opposite decoding.

Without doing that, for instance, the line breaks disappear in the message received on the other mobile.

Code on iOS:

NSData *data1 = [myStringTosend dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding] autorelease];

And decoding:

NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];

So far (and not correct), encoding on the Android side:

OutputStream os = socket.getOutputStream();
os.write(request.getBytes("UTF-8"));
os.flush();

And decoding:

while ((bytesRead = is.read(buffer, 0, BUFFER_SIZE)) >= 0) {
    if (bytesRead > 0) response.append(new String(buffer, 0, bytesRead, "UTF-8"));
    if (bytesRead < BUFFER_SIZE) break;
}


@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.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK