package org.ingrid.util;

/**
 *	UnicodeTool class has utility methods that create a string
 *	representation of the Unicode as an unsigned integer in base
 *	16.
 *
 *	@version 1.0
 *	@author Kazuhiro Kazama
 */
public final class UnicodeTool {

    /**
     * This method is used for test.
     */
    public static void main(String args[]) {
	String s = "ABCDEF\u3042\u3044\u3046\u3048\u304a";
	StringBuffer sb = new StringBuffer(s);
	char c[] = s.toCharArray();
	byte b[] = s.getBytes();

	System.out.println(s);
	System.out.println(UnicodeTool.toHexString(s));
	System.out.println(UnicodeTool.toHexString(sb));
	System.out.println(UnicodeTool.toHexString(c));
	System.out.println(UnicodeTool.toHexString(c[0]));
	System.out.println(UnicodeTool.toHexString(b));
	System.out.println(UnicodeTool.toHexString(b[0]));
    }

    private static String toUnsignedString(int i, int j) {
	StringBuffer sb = new StringBuffer();
	do {
	    sb.append(Character.forDigit(i & 0x0f, 16));
	    i >>>= 4;
	} while (--j > 0);
	return new String(sb.reverse());
    }

    /**
     * Creates a string representation of the byte argument as an 
     * unsigned integer in base&nbsp;16.
     *
     * @param b a byte
     * @return the string representation of the byte value represented
     * by the argument in hexadecimal (base&nbsp; 16).
     */
    public static String toHexString(byte b) {
	return toUnsignedString((int)b, 2);
    }

    /**
     * Creates a string representation of the byte array argument as an 
     * unsigned integer in base&nbsp;16.
     *
     * @param b a byte array
     * @return the string representation of the byte array represented
     * by the argument in hexadecimal (base&nbsp; 16).
     */
    public static String toHexString(byte[] b) {
	StringBuffer sb = new StringBuffer();
	for (int i = 0; i < b.length; i++) {
	    if (i > 0)
		sb.append(' ');
	    sb.append(toHexString(b[i]));
	}
	return new String(sb);
    }

    /**
     * Creates a string representation of the char argument as an 
     * unsigned integer in base&nbsp;16.
     *
     * @param c a char
     * @return the string representation of the char value represented
     * by the argument in hexadecimal (base&nbsp; 16).
     */
    public static String toHexString(char c) {
	return toUnsignedString((int)c, 4);
    }

    /**
     * Creates a string representation of the char argument as an 
     * unsigned integer in base&nbsp;16.
     *
     * @param c a char array
     * @return the string representation of the char value represented
     * by the argument in hexadecimal (base&nbsp; 16).
     */
    public static String toHexString(char[] c) {
	StringBuffer sb = new StringBuffer();
	for (int i = 0; i < c.length; i++) {
	    if (i > 0)
		sb.append(' ');
	    sb.append(toHexString(c[i]));
	}
	return new String(sb);
    }

    /**
     * Creates a string representation of the String argument as an 
     * unsigned integer in base&nbsp;16.
     *
     * @param s a String
     * @return the string representation of the String value represented
     * by the argument in hexadecimal (base&nbsp; 16).
     */
    public static String toHexString(String s) {
	StringBuffer sb = new StringBuffer();
	for (int i = 0; i < s.length(); i++) {
	    if (i > 0)
		sb.append(' ');
	    sb.append(toHexString(s.charAt(i)));
	}
	return new String(sb);
    }

    /**
     * Creates a string representation of the StringBuffer argument as an 
     * unsigned integer in base&nbsp;16.
     *
     * @param s a StringBuffer
     * @return the string representation of the StringBuffer value represented
     * by the argument in hexadecimal (base&nbsp; 16).
     */
    public static String toHexString(StringBuffer s) {
	StringBuffer sb = new StringBuffer();
	for (int i = 0; i < s.length(); i++) {
	    if (i > 0)
		sb.append(' ');
	    sb.append(toHexString(s.charAt(i)));
	}
	return new String(sb);
    }
}
