Converting byte array to String (Java)

前端 未结 7 1085
不知归路
不知归路 2020-11-28 06:29

I\'m writing a web application in Google app Engine. It allows people to basically edit html code that gets stored as an .html file in the blobstore.

I\

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 07:00

    The byte array contains characters in a special encoding (that you should know). The way to convert it to a String is:

    String decoded = new String(bytes, "UTF-8");  // example for one encoding type
    

    By The Way - the raw bytes appear may appear as negative decimals just because the java datatype byte is signed, it covers the range from -128 to 127.


    -109 = 0x93: Control Code "Set Transmit State"
    

    The value (-109) is a non-printable control character in UNICODE. So UTF-8 is not the correct encoding for that character stream.

    0x93 in "Windows-1252" is the "smart quote" that you're looking for, so the Java name of that encoding is "Cp1252". The next line provides a test code:

    System.out.println(new String(new byte[]{-109}, "Cp1252")); 
    

提交回复
热议问题