Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I encode/decode UTF-16LE byte arrays with a BOM?
    text
    copied!<p>I need to encode/decode UTF-16 byte arrays to and from <code>java.lang.String</code>. The byte arrays are given to me with a <a href="http://unicode.org/faq/utf_bom.html#bom1" rel="noreferrer">Byte Order Marker (BOM)</a>, and I need to encoded byte arrays with a BOM.</p> <p>Also, because I'm dealing with a Microsoft client/server, I'd like to emit the encoding in little endian (along with the LE BOM) to avoid any misunderstandings. I do realize that with the BOM it should work big endian, but I don't want to swim upstream in the Windows world.</p> <p>As an example, here is a method which encodes a <code>java.lang.String</code> as <code>UTF-16</code> in little endian with a BOM:</p> <pre><code>public static byte[] encodeString(String message) { byte[] tmp = null; try { tmp = message.getBytes("UTF-16LE"); } catch(UnsupportedEncodingException e) { // should not possible AssertionError ae = new AssertionError("Could not encode UTF-16LE"); ae.initCause(e); throw ae; } // use brute force method to add BOM byte[] utf16lemessage = new byte[2 + tmp.length]; utf16lemessage[0] = (byte)0xFF; utf16lemessage[1] = (byte)0xFE; System.arraycopy(tmp, 0, utf16lemessage, 2, tmp.length); return utf16lemessage; } </code></pre> <p>What is the best way to do this in Java? Ideally I'd like to avoid copying the entire byte array into a new byte array that has two extra bytes allocated at the beginning.</p> <p>The same goes for decoding such a string, but that's much more straightforward by using the <a href="http://file:///Users/jared/Documents/jdk-6u10-docs/api/java/lang/String.html#String(byte[],%20int,%20int,%20java.lang.String)" rel="noreferrer"><code>java.lang.String</code> constructor</a>:</p> <pre><code>public String(byte[] bytes, int offset, int length, String charsetName) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload