Java UTF16 Convert toUTF16LEString(String input)

Here you can find the source of toUTF16LEString(String input)

Description

Convert the input string to String encoded in UTF16LE format.

License

Open Source License

Parameter

Parameter Description
input a parameter

Exception

Parameter Description
IOException an exception

Return

String encoded in UTF16LE format

Declaration

public static String toUTF16LEString(String input) throws IOException 

Method Source Code


//package com.java2s;
import java.io.IOException;

public class Main {
    /**//w w w . jav  a2s.  com
     * Convert the input string to String encoded in UTF16LE format.
     * 
     * @param input
     * @return String encoded in UTF16LE format
     * @throws IOException
     */
    public static String toUTF16LEString(String input) throws IOException {
        if (input == null || input.length() == 0) {
            return input;
        }

        byte[] b = input.getBytes("UTF-16LE");
        return hexToString(b);
    }

    /**
     * Converts a byte array to string.
     * 
     * @param data
     *            Input byte array.
     * @return String
     */
    public static String hexToString(byte[] data) {
        if (data == null) {
            return "";
        }
        ;
        StringBuffer sb = new StringBuffer(data.length * 2);
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(0xFF & data[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString().toUpperCase();
    }

    /**
     * Converts object to a string.<br>
     * If object is null then return null
     * 
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        if (null == obj) {
            return null;
        } else {
            return obj.toString();
        }
    }
}

Related

  1. UTF16toUTF8(CharSequence s, int offset, int len, byte[] result, int resultOffset)