UTF-16BE to UTF-16LE and vice versa

I have a Blackberry project that I'm working on, and I need to convert byte arrays of strings encoded using UTF-16LE (little endian) to a byte array of strings encoded in UTF-16BE (big endian) and visible the other way around. The server I'm connecting to sends BlackBerry byte arrays in UTF-16LE encoding, but the device does not support UTF-16LE. When I try to decode byte arrays back to strings, strings are illegible. However, the device supports UTF-16BE. I also need to reverse this process, that is, convert the array of bytes of the UTF-16BE encoded string to what the server expects (UTF-16LE). Thanks.

I can not do this on the device:

String test = "test"; byte[] testBytes = test.getBytes("UTF-16LE");// throws UnsupportedEncodingException 

I can do this:

 String test = "test"; byte[] testBytes = test.getBytes("UTF-16BE");//works 
+6
source share
1 answer

UTF-16 uses two bytes per code block, with some Unicode encodings being encoded using one code and other code points using two code elements (called a surrogate pair).

To convert between UTF-16LE and UTF-16BE, simply loop through the bytes by swapping the order of each double-byte pair of each code element. The order of the surrogate codes does not change between LE and BE. IOW, just change bytes 0 and 1 with each other, change bytes 2 and 3 with each other, etc.

+11
source

Source: https://habr.com/ru/post/923603/


All Articles