/**
* Convert a byte array to a hex encoded string
*
* @param block
* byte array to convert to hexString
* @return String representation of byte array
*/ publicstaticString toHex(byte[] block) { StringBuffer buf =newStringBuffer();
for(int i = 0; i < block.length;++i) {
buf.append(hexits.charAt((block[i]>>> 4)& 0xf));/* hell: high 4 bits */
buf.append(hexits.charAt(block[i]& 0xf));/* hell: low 4 bits */ }
return buf +""; }
/**
* Convert a String hex notation to a byte array
*
* @param s
* string to convert
* @return byte array
*/ publicstaticbyte[] fromHex(String s){
s = s.toLowerCase(Locale.ENGLISH); byte[] b =newbyte[(s.length()+ 1)/ 2]; int j = 0; int h; int nibble =-1;
for(int i = 0; i < s.length();++i) {
h = hexits.indexOf(s.charAt(i)); if(h >= 0) { if(nibble < 0)/* hell: nibble store high 4 bits */ {
nibble = h; } else {
b[j++]=(byte)((nibble << 4)+ h);
nibble =-1; } } }