How to Convert Byte Array to String in Java

xiaoxiao2021-02-28  20

This tutorial demonstrates how to convert a ·byte[]· to String.

Encoding this String into a sequence of bytes using the given Charset, storing the result into a new byte array.

byte[] byteArray = "this is a string".getBytes(StandardCharsets.UTF_8);

Constructs a new String by decoding the specified array of bytes using the specified Charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

String converted = new String(byteArray, StandardCharsets.UTF_8);

注意: 解码和编码的字符集要一直。

转载请注明原文地址: https://www.6miu.com/read-2602668.html

最新回复(0)