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:
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"
Note that this method assumes that the byte array is encoded in UTF-8. If the byte array is not encoded in UTF-8, this method will not produce the correct result.
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(bytes):
return bytes.decode('utf-8')
Here is an example of how to use this method:
bytes = b'\xC3\xA9 \xC3\xA7a'
s = utf8_byte_array_to_string(bytes)
print(s) # Output: "é ça"
Again, this method assumes that the byte array is encoded in UTF-8. If the byte array is not encoded in UTF-8, this method will not produce the correct result.