Java Unicode Create getUnicodeBytes(String s)

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

Description

Converts the string into a little-endian array of Unicode bytes

License

Open Source License

Parameter

Parameter Description
s the string to convert

Return

the unicode values of the characters in the string

Declaration

public static byte[] getUnicodeBytes(String s) 

Method Source Code


//package com.java2s;
/* /*www .  j a v a  2  s. c  o m*/
 *FastExcel,(c) copyright 2009 yAma<guooscar@gmail.com>.  
 *WEB: http://fastexcel.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    public static final String UNICODE_ENCODING = "UnicodeLittle";

    /**
     * Converts the string into a little-endian array of Unicode bytes
     * 
     * @param s
     *            the string to convert
     * @return the unicode values of the characters in the string
     */
    public static byte[] getUnicodeBytes(String s) {
        try {
            byte[] b = s.getBytes(UNICODE_ENCODING);
            // Sometimes this method writes out the unicode
            // identifier
            if (b.length == (s.length() * 2 + 2)) {
                byte[] b2 = new byte[b.length - 2];
                System.arraycopy(b, 2, b2, 0, b2.length);
                b = b2;
            }
            return b;
        } catch (UnsupportedEncodingException e) {
            // Fail silently
            return null;
        }
    }

    /**
     * Converts the string into a little-endian array of UNICODE bytes
     * 
     * @param s
     *            the string to convert
     * @param bytes
     *            result holder.
     * @param offset
     *            offset to result
     * @return the UNICODE bytes's length.
     */
    public static int getUnicodeBytes(String s, byte bytes[], int offset) {
        byte[] bb = getUnicodeBytes(s);
        System.arraycopy(bb, 0, bytes, offset, bb.length);
        return bb.length;
    }

    /**
     * Gets the bytes of the specified string. This will simply return the ASCII
     * values of the characters in the string
     * 
     * @param s
     *            the string to convert into bytes
     * @return the ASCII values of the characters in the string
     */
    public static byte[] getBytes(String s) {
        return s.getBytes();
    }

    public static int getBytes(String s, byte bytes[], int offset) {
        byte[] bb = getBytes(s);
        System.arraycopy(bb, 0, bytes, offset, bb.length);
        return bb.length;
    }
}

Related

  1. getUnicodeByteArray(String str)
  2. getUnicodeString(byte bytes[], int offset, int len)
  3. getUnicodeString(byte[] d, int length, int pos)
  4. getUnicodeText(Object text)
  5. toUnicode(char c)