简体   繁体   中英

Android/Java: convert UTF-8 to UTF-16

I am developing Android App and I need to communicate to local server via TCP/IP. We used JSON (I'm using Jackson parser for Android) to pass the message to the server.

When I do ObjectMapper.writeValueAsBytes() using jackson it produces bytes in UTF-8? Our local server is written in C# .NET and the uses UTF-16 Little Endian encoding.

When the bytes arrived at the server, it unable to parse the JSON. When I debug the server by convert the json byte to string, I can see some part of the string are scrambled. I suspect because of this UTF-8 to UTF-16 Little Endian problem.

My question is, how do I convert these json bytes that is in UTF-8 to UTF-16 Little Endian before I pass it to the server.

Thanks

Something like

byte[] utf8bytes = .......
byte[] utf16bytes = (new String(utf8bytes, "utf-8")).getBytes("utf-16");

Untested.

You can try UTF-16LE and UTF-16BE for little and big endian.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM