java: can I convert strings with String.getBytes() without the BOM?
Posted
by Cheeso
on Stack Overflow
See other posts from Stack Overflow
or by Cheeso
Published on 2010-03-28T03:40:32Z
Indexed on
2010/03/28
3:43 UTC
Read the original article
Hit count: 463
Suppose I have this code:
String encoding = "UTF-16";
String text = "[Hello StackOverflow]";
byte[] message= text.getBytes(encoding);
If I display the byte array in message, the result is:
0000 FE FF 00 5B 00 48 00 65 00 6C 00 6C 00 6F 00 20 ...[.H.e.l.l.o.
0010 00 53 00 74 00 61 00 63 00 6B 00 4F 00 76 00 65 .S.t.a.c.k.O.v.e
0020 00 72 00 66 00 6C 00 6F 00 77 00 5D .r.f.l.o.w.]
As you can see, there's a BOM in the beginning.
How can I:
- generate a UTF-16 byte array that lacks a BOM ?
- convert from a byte array that contains UTF=16 chars but lacks a BOM, back to a string?
© Stack Overflow or respective owner