Here you can find the source of toUnicodeString(byte[] ba)
Similar to the toString()
method except that the Unicode escape character is inserted before every pair of bytes.
Parameter | Description |
---|---|
ArrayIndexOutOfBoundsException | if the length is odd. |
public static String toUnicodeString(byte[] ba)
//package com.java2s; // it under the terms of the GNU General Public License as published by public class Main { private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray(); /**//from ww w .j a v a 2s . com * <p>Similar to the <code>toString()</code> method except that the Unicode * escape character is inserted before every pair of bytes. Useful to * externalise byte arrays that will be constructed later from such strings; * eg. s-box values.</p> * * @throws ArrayIndexOutOfBoundsException if the length is odd. */ public static String toUnicodeString(byte[] ba) { return toUnicodeString(ba, 0, ba.length); } /** * <p>Similar to the <code>toString()</code> method except that the Unicode * escape character is inserted before every pair of bytes. Useful to * externalise byte arrays that will be constructed later from such strings; * eg. s-box values.</p> * * @throws ArrayIndexOutOfBoundsException if the length is odd. */ public static final String toUnicodeString(byte[] ba, int offset, int length) { StringBuffer sb = new StringBuffer(); int i = 0; int j = 0; int k; sb.append('\n').append("\""); while (i < length) { sb.append("\\u"); k = ba[offset + i++]; sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]); sb.append(HEX_DIGITS[k & 0x0F]); k = ba[offset + i++]; sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]); sb.append(HEX_DIGITS[k & 0x0F]); if ((++j % 8) == 0) { sb.append("\"+").append('\n').append("\""); } } sb.append("\"").append('\n'); return sb.toString(); } /** * <p>Similar to the <code>toString()</code> method except that the Unicode * escape character is inserted before every pair of bytes. Useful to * externalise integer arrays that will be constructed later from such * strings; eg. s-box values.</p> * * @throws ArrayIndexOutOfBoundsException if the length is not a multiple of 4. */ public static String toUnicodeString(int[] ia) { StringBuffer sb = new StringBuffer(); int i = 0; int j = 0; int k; sb.append('\n').append("\""); while (i < ia.length) { k = ia[i++]; sb.append("\\u"); sb.append(HEX_DIGITS[(k >>> 28) & 0x0F]); sb.append(HEX_DIGITS[(k >>> 24) & 0x0F]); sb.append(HEX_DIGITS[(k >>> 20) & 0x0F]); sb.append(HEX_DIGITS[(k >>> 16) & 0x0F]); sb.append("\\u"); sb.append(HEX_DIGITS[(k >>> 12) & 0x0F]); sb.append(HEX_DIGITS[(k >>> 8) & 0x0F]); sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]); sb.append(HEX_DIGITS[k & 0x0F]); if ((++j % 4) == 0) { sb.append("\"+").append('\n').append("\""); } } sb.append("\"").append('\n'); return sb.toString(); } /** * <p>Returns a string of hexadecimal digits from a byte array. Each byte is * converted to 2 hex symbols; zero(es) included.</p> * * <p>This method calls the method with same name and three arguments as:</p> * * <pre> * toString(ba, 0, ba.length); * </pre> * * @param ba the byte array to convert. * @return a string of hexadecimal characters (two for each byte) * representing the designated input byte array. */ public static String toString(byte[] ba) { return toString(ba, 0, ba.length); } /** * <p>Returns a string of hexadecimal digits from a byte array, starting at * <code>offset</code> and consisting of <code>length</code> bytes. Each byte * is converted to 2 hex symbols; zero(es) included.</p> * * @param ba the byte array to convert. * @param offset the index from which to start considering the bytes to * convert. * @param length the count of bytes, starting from the designated offset to * convert. * @return a string of hexadecimal characters (two for each byte) * representing the designated input byte sub-array. */ public static final String toString(byte[] ba, int offset, int length) { char[] buf = new char[length * 2]; for (int i = 0, j = 0, k; i < length;) { k = ba[offset + i++]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[k & 0x0F]; } return new String(buf); } /** * <p>Returns a string of 8 hexadecimal digits (most significant digit first) * corresponding to the unsigned integer <code>n</code>.</p> * * @param n the unsigned integer to convert. * @return a hexadecimal string 8-character long. */ public static String toString(int n) { char[] buf = new char[8]; for (int i = 7; i >= 0; i--) { buf[i] = HEX_DIGITS[n & 0x0F]; n >>>= 4; } return new String(buf); } /** * <p>Returns a string of hexadecimal digits from an integer array. Each int * is converted to 4 hex symbols.</p> */ public static String toString(int[] ia) { int length = ia.length; char[] buf = new char[length * 8]; for (int i = 0, j = 0, k; i < length; i++) { k = ia[i]; buf[j++] = HEX_DIGITS[(k >>> 28) & 0x0F]; buf[j++] = HEX_DIGITS[(k >>> 24) & 0x0F]; buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F]; buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F]; buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F]; buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[k & 0x0F]; } return new String(buf); } /** * <p>Returns a string of 16 hexadecimal digits (most significant digit first) * corresponding to the unsigned long <code>n</code>.</p> * * @param n the unsigned long to convert. * @return a hexadecimal string 16-character long. */ public static String toString(long n) { char[] b = new char[16]; for (int i = 15; i >= 0; i--) { b[i] = HEX_DIGITS[(int) (n & 0x0FL)]; n >>>= 4; } return new String(b); } }