1 22 23 24 25 package org.webdocwf.util.loader; 26 27 import java.io.*; 28 29 35 public class ToAndFromHex { 36 37 40 public ToAndFromHex() { 41 } 42 43 48 public static String getStringFromBlob(byte[] b){ 49 50 if(b!=null){ 51 ByteArrayInputStream is=new ByteArrayInputStream(b); 52 53 char[] hexBytes = { 54 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 55 }; 56 57 int c; 58 String hexString=new String (); 59 while ((c = is.read()) >= 0) { 60 hexString+=(hexBytes[(c >> 4) & 0xf]); 61 hexString+=(hexBytes[c & 0xf]); 62 } 63 return hexString; 64 }else{ 65 return null; 66 } 67 } 68 69 74 public static byte[] getByteArrayFromString(String val){ 75 byte[] buf = new byte[val.length() / 2]; 76 final char[] hexBytes = { 77 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 78 }; 79 byte[] hexMap = new byte[256]; 80 for (int i = 0; i < hexBytes.length; i++) { 81 hexMap[hexBytes[i]] = (byte)i; 82 } 83 int pos = 0; 84 for (int i = 0; i < buf.length; i++) { 85 buf[i] = (byte)(hexMap[val.charAt(pos++)] << 4); 86 buf[i] += hexMap[val.charAt(pos++)]; 87 } 88 return buf; 89 } 90 91 92 } 93 | Popular Tags |