Friday 7 February 2020

Convert UTF_16LE byte array to string

Following snippet converts UTF_16LE byte array to string.

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

App.java
package com.sample.app;

import java.nio.charset.StandardCharsets;

public class App {

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

  System.out.println(str);

 }

} 

Output
Hello World


You may like

No comments:

Post a Comment