W3docs

UTF-8 byte[] to String

To convert a byte array to a string in UTF-8 encoding, you can use the following method in Java:

To convert a byte array to a string in UTF-8 encoding, you can use the following method in Java:


import java.nio.charset.StandardCharsets;

public static String utf8ByteArrayToString(byte[] bytes) {
    return new String(bytes, StandardCharsets.UTF_8);
}

Here is an example of how to use this method:


byte[] bytes = {(byte) 0xC3, (byte) 0xA9, (byte) 0x20, (byte) 0xC3, (byte) 0xA7, (byte) 0x61};
String s = utf8ByteArrayToString(bytes);
System.out.println(s);  // Output: "é ça"

In Python, you can use the following code to convert a byte array to a string in UTF-8 encoding:


def utf8_byte_array_to_string(raw_bytes):
    return raw_bytes.decode('utf-8')

Here is an example of how to use this method:


raw_bytes = b'\xC3\xA9 \xC3\xA7a'
s = utf8_byte_array_to_string(raw_bytes)
print(s)  # Output: "é ça"

Note that both methods assume the input is encoded in UTF-8. If the byte array is not UTF-8 encoded, the result will be incorrect. For reference, the hex values \xC3\xA9 and \xC3\xA7 are the UTF-8 byte sequences for 'é' and 'ç' respectively.