package com.fitbank.common.crypto; public class Util { private static char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static String byteArray2Hex(byte[] ba){ StringBuffer sb = new StringBuffer(); for (int i = 0; i < ba.length; i++){ int hbits = (ba[i] & 0x000000f0) >> 4; int lbits = ba[i] & 0x0000000f; sb.append("" + hexChars[hbits] + hexChars[lbits] + ""); } return sb.toString(); } public static byte[] decodeHex(char[] data) { int len = data.length; if ((len & 0x01) != 0) { //System.out.println("Odd number of characters."); } byte[] out = new byte[len >> 1]; // two characters form the hex value. for (int i = 0, j = 0; j < len; i++) { int f = toDigit(data[j], j) << 4; j++; f = f | toDigit(data[j], j); j++; out[i] = (byte) (f & 0xFF); } return out; } protected static int toDigit(char ch, int index) { int digit = Character.digit(ch, 16); if (digit == -1) { //System.out.println("Illegal hexadecimal charcter " + ch + " at index " + index); } return digit; } }