NSNonLossyASCIIStringEncoding相当于为AndroidNSNonLossyASCIIStringEncoding、Android

2023-09-07 15:30:23 作者:借我、你的一輩子

我到港的一些聊天code从的iOS到Android。在发送聊天消息,插座,iOS的code使用 NSNonLossyASCIIStringEncoding 类作为的NSString :: dataUsingEncoding的参数。

你怎么会做它的Andr​​oid?左右相反的解码同样的问题。

如果没有这样做,例如,对其他移动接收的消息中的换行消失。

在iOS code:

 的NSData *数据1 = [myStringTosend dataUsingEncoding:NSNonLossyASCIIStringEncoding]。
的NSString * goodValue = [[[NSString的页头] initWithData:数据1编码:NSUTF8StringEncoding]自动释放]。
 

和解码:

 的NSData *数据= [[NSData的页头] initWithData:[回应dataUsingEncoding:NSASCIIStringEncoding];
 

到目前为止(不正确),在Android端编码:

 的OutputStream OS = socket.getOutputStream();
os.write(request.getBytes(UTF-8));
os.flush();
 
负债21.8万亿美元,1天利息15亿 怎么办 特朗普一句话让人

和解码:

 ,而((读取动作= is.​​read(缓冲,0,BUFFER_SIZE))> = 0){
    如果(读取动作大于0)response.append(新字符串(缓冲液,0,读取动作,UTF-8));
    如果(读取动作< BUFFER_SIZE)破;
}
 

解决方案

不要使用NSNonLossyASCIIStringEncoding,使用UTF-8编码。我只是解决了这个问题,我自己在IOS +安卓+ java的春天后台,我花了大约4天的时间把事情做好。 Android无法显示emojis,但是这给了我完整的字符支持几乎所有(或所有不确定)语言。下面是帮助过我的文章:

必读:的http://博客.manbolo.com / 2012/10/29 /支持,新emojis-ON-IOS-6 的 http://blog.manbolo.com/2011/12/12/supporting-ios-5-new-emoji-encoding

请参阅十六进制字节的字符串数据库里面的:How我可以看到存储在MySQL列原始字节?

HTTP:

有关如何安装MySQL的详细信息://technovergence-en.blogspot.com/2012/03/mysql-from-utf8-to-utf8mb4.html

在utf8- HTTP深度FAQ:// WWW。 UNI code.org / FAQ / utf_bom.html#utf8-4

\ ud83d \ udc7d和内存中的十六进制值:0xF09F91BD

有关从符号的差异详情http://en.wikipedia.org/wiki/UTF-8#Description

使用此项以复制和粘贴文字,看看真正的十六进制字节值(适用于emojis):的 HTTP://perishable$p$pss.com/tool​​s/utf8-hex/index.php

让Spring支持UTF8的网址(用于GET参数)的http://forum.springsource.org/showthread.php?93728-RequestParam-doesn-t-seem-to-be-de$c$cd 获取参数编码 http://forum.springsource.org/showthread.php?112181-Unable-to-Override-the-Spring-MVC-URL-decoding-which-uses-default-quot-ISO-8859-1-quot

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;
}

解决方案

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