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

后端 未结 4 1877
半阙折子戏
半阙折子戏 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条回答
  •  执念已碎
    2021-01-17 18:54

    The encoded values you are getting, [-17, -65, -67] correspond to Unicode code point 0xFFFD. If you look up that code point, the Unicode specification tells you that 0XFFFD "used to replace an incoming character whose value is unknown or unrepresentable in Unicode." And as others have pointed out, -3 without any followup code-units is broken UTF-8, so this character is appropriate.

提交回复
热议问题