What happens under the hood when bytes converted to String in Java?

后端 未结 4 1873
半阙折子戏
半阙折子戏 2021-01-17 17:57

I have a problem when trying to convert bytes to String in Java, with code like:

byte[] bytes = {1, 2, -3};

byte[] transferred = new String(bytes, Charsets.         


        
4条回答
  •  -上瘾入骨i
    2021-01-17 18:58

    In Java, byte is signed, where negative values are above 127. And those you used (-3 = 0xFD, -32 = 0xE0) are not valid in UTF-8, so they both are converted to Unicode codepoint U+FFFD REPLACEMENT CHARACTER, which is converted back to UTF-8 as 0xEF = -17, 0xBF = -65, 0xBD = -67.

    You cannot expect that random byte values are correctly interpreted as UTF-8 text.

提交回复
热议问题