Tuesday 28 January 2020

Java: Convert UTF-8 byte array to string

Following snippet converts UTF-8 byte array to string.

byte[] byteArr = {72,101,108,108,111,32,87,111,114,108,100};
String str = new String(byteArr, StandardCharsets.UTF_8);

App.java
package com.sample.app;

import java.nio.charset.StandardCharsets;

public class App {

 public static void main(String[] args) {
  byte[] byteArr = {72,101,108,108,111,32,87,111,114,108,100};
  String str = new String(byteArr, StandardCharsets.UTF_8);
  
  System.out.println(str);
  
 }

}

Output
Hello World



You may like

No comments:

Post a Comment