package com.eactive.eai.util;

public class HexaConverter {
    public static final String bytesToHexa(byte[] abyte) {
        StringBuilder s = new StringBuilder();
        if (abyte == null)
            return s.toString();
        for (int i = 0; i < abyte.length; i++) {
            s.append( Integer.toHexString((abyte[i] & 0xf0) >> 4) )
             .append( Integer.toHexString(abyte[i] & 0xf));
        }    
        return s.toString();
    }
    
    public static final byte[] hexaToBytes(String hexa) {
    	if((hexa.length() % 2) != 0) return null;
    	byte[] bytes = new byte[hexa.length() / 2];
    	
    	String hexaBytes = null;
    	for(int i=0; i <bytes.length;i++) {
    		hexaBytes = hexa.substring(i*2, i*2+2);
    		int byteBit = Integer.parseInt(hexaBytes, 16);
    		bytes[i] = (byte)byteBit;
    	}
    	return bytes;
    }
    
//    public static void main(String[] argv) {
//    	String str = "TEST\nÇÑ±Û";
//    	String hexStr = HexaConverter.bytesToHexa(str.getBytes());
//    	System.out.println(hexStr);
//    	System.out.println(new String(HexaConverter.hexaToBytes(hexStr), Charset.forName("euc-kr") ) );
//    	System.out.println(new String(HexaConverter.hexaToBytes(hexStr)) );    	
//    }
}
