How to convert UTF-8 byte[] to string in Java

1 Answer

0 votes
import java.nio.charset.StandardCharsets;

public class MyClass {
    public static String utf8ByteArrayToString(byte[] bytes) {
        return new String(bytes, StandardCharsets.UTF_8);
    }
    public static void main(String args[]) {
        byte[] bytes = {(byte) 0xC3, (byte) 0xA9, (byte) 0x20, (byte) 0x61, (byte) 0x62};
        
        String str = utf8ByteArrayToString(bytes);
        
        System.out.println(str);  
    }
}



/*
run:

é ab

*/

 



answered Nov 5, 2023 by avibootz

Related questions

...