How to convert byte[] type to String type in java

To convert byte[] type to String type in Java, you can use String's constructor or static method.

  1. Use the String constructor

byte[] byteArray = {97, 98, 99}; String str = new String(byteArray);

     2. Use the static method valueOf()

byte[] byteArray = {97, 98, 99}; String str = String.valueOf(byteArray);

It should be noted that the character set needs to be specified during the conversion process, otherwise the platform default character set will be used for conversion. If the byte array contains UTF-8 encoded strings, you should use "UTF-8" to specify the character set:

byte[] byteArray = {97, 98, 99}; String str = new String(byteArray, "UTF-8");

byte[] byteArray = {97, 98, 99}; String str = String.valueOf(byteArray, "UTF-8");

おすすめ

転載: blog.csdn.net/gb4215287/article/details/131174109