Android Unicode Convert bytes2StringUNICODE(byte[] buf, int offset, int length, boolean bigEndian)

Here you can find the source of bytes2StringUNICODE(byte[] buf, int offset, int length, boolean bigEndian)

Description

format the byte[] to String in UNICODE encode

Parameter

Parameter Description
buf a parameter
bigEndian a parameter

Declaration

public static String bytes2StringUNICODE(byte[] buf, int offset,
        int length, boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  www .j av a  2  s  .  com*/
     * format the byte[] to String in UNICODE encode
     * @param buf
     * @param bigEndian
     * @return
     */
    public static String bytes2StringUNICODE(byte[] buf, int offset,
            int length, boolean bigEndian) {
        if (buf != null && offset >= 0 && length >= 2
                && buf.length >= (offset + length)) {
            int charsLen = length / 2;
            char[] cbuf = new char[charsLen];
            for (int i = 0; i < charsLen; i++) {
                if (bigEndian) {
                    cbuf[i] = (char) (((buf[i * 2 + offset] & 0xff) << 8 | (buf[i
                            * 2 + 1 + offset] & 0xff)) & 0xffff);
                } else {
                    cbuf[i] = (char) (((buf[i * 2 + 1 + offset] & 0xff) << 8 | (buf[i
                            * 2 + offset] & 0xff)) & 0xffff);
                }
            }
            String str = new String(cbuf, 0, charsLen);
            cbuf = null;
            return str;
        }

        return null;
    }
}

Related

  1. unicodePreservingIndex(String paramString, int paramInt)
  2. unicodePreservingIndex(String str, int index)
  3. fullWidthToHalfWidth(String s)
  4. getEncodedSize(String value)
  5. halfWidthToFullWidth(String s)
  6. asciiToBCD(byte[] ascii_buf, int asc_offset, byte[] bcd_buf, int bcd_offset, int conv_len, int type)
  7. convertToUnicodeByteArray(String s)
  8. convertToUnicode(byte[] b, boolean includesNull)
  9. stringToUnicode(String strText)