You can save the byte in a string, although this is not a good idea. You cannot use UTF-8, as this will lead to a change in bytes, but a faster and more efficient way is to use the encoding ISO-8859-1 or simple 8-bit. The easiest way to do this is to use
String s1 = new String(data, 0);
or
String s1 = new String(data, "ISO-8859-1");
From WTF-8 on Wikipedia , as John Skeet notes, these encodings are not standard. Their behavior in Java is changing. The DataInputStream treats them as the same for the first three versions, and the next two throw an exception. The Charset decoder treats them as single characters in silence.
00000000 is \0 11000000 10000000 is \0 11100000 10000000 10000000 is \0 11110000 10000000 10000000 10000000 is \0 11111000 10000000 10000000 10000000 10000000 is \0 11111100 10000000 10000000 10000000 10000000 10000000 is \0
This means that if you see \ 0 in you String, you cannot know exactly what the values of the original byte [] are. DataOutputStream uses the second option for compatibility with C, which sees \ 0 as a terminator.
BTW DataOutputStream does not know about code points, so it writes high-code characters in UTF-16, and then UTF-8 encoding.
0xFE and 0xFF are not valid for display in the character. The values 0x11000000 + can only appear at the beginning of a character, and not inside a multibyte character.
source share