Here you can find the source of getUnicodeBytes(String s)
Parameter | Description |
---|---|
s | the string to convert |
public static byte[] getUnicodeBytes(String s)
//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; } }