Sam
Sam

Reputation: 1908

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

Upvotes: 1

Views: 2560

Answers (1)

greenapps
greenapps

Reputation: 11224

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.

Upvotes: 2

Related Questions