Android Unicode Convert convertToUnicodeByteArray(String s)

Here you can find the source of convertToUnicodeByteArray(String s)

Description

Converts the String to a UNICODE byte array.

License

Open Source License

Parameter

Parameter Description
s the string to convert

Return

the unicode byte array of the string

Declaration

public static byte[] convertToUnicodeByteArray(String s) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w  w w  . j  a  v a  2  s.c  o m*/
     * Converts the String to a UNICODE byte array. It will also add the ending
     * null characters to the end of the string.
     * @param s the string to convert
     * @return the unicode byte array of the string
     */
    public static byte[] convertToUnicodeByteArray(String s) {
        if (s == null) {
            return null;
        }

        char c[] = s.toCharArray();
        byte[] result = new byte[(c.length * 2) + 2];
        for (int i = 0; i < c.length; i++) {
            result[(i * 2)] = (byte) (c[i] >> 8);
            result[((i * 2) + 1)] = (byte) c[i];
        }

        // Add the UNICODE null character
        result[result.length - 2] = 0;
        result[result.length - 1] = 0;

        return result;
    }
}

Related

  1. fullWidthToHalfWidth(String s)
  2. getEncodedSize(String value)
  3. halfWidthToFullWidth(String s)
  4. bytes2StringUNICODE(byte[] buf, int offset, int length, boolean bigEndian)
  5. asciiToBCD(byte[] ascii_buf, int asc_offset, byte[] bcd_buf, int bcd_offset, int conv_len, int type)
  6. convertToUnicode(byte[] b, boolean includesNull)
  7. stringToUnicode(String strText)
  8. unicode2han3last_direct(Character c)
  9. unicode2han_str(String str)