日期:2014-05-20 浏览次数:20807 次
public static byte[] objectToByte(Object obj) throws IOException {
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buff);
out.writeObject(obj);
try {
return buff.toByteArray();
} finally {
out.close();
}
}
public static Object byteToObject(byte[] b)
throws IOException, ClassNotFoundException {
ByteArrayInputStream buff = new ByteArrayInputStream(b);
ObjectInputStream in = new ObjectInputStream(buff);
Object obj = in.readObject();
try {
return obj;
} finally {
in.close();
}
}
public class PrivacyUtil {
/**
* hex table
*/
private static char[] hexChars = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
/**
* do not allow instantiation.
*/
private PrivacyUtil() {
}
/**
* bytes to string.
* @param b src bytes。
* @return hex string。
*/
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(hexChars[(b[i] & 0xf0) >>> 4]);
sb.append(hexChars[b[i] & 0x0f]);
}
return sb.toString();
}
/**
* hex string to bytes
* @param hexString hex string.
* @return src bytes.
*/
public static byte[] toOriginalByte(String hexString) {
if (hexString == null
------解决方案--------------------
hexString.isEmpty()) {
return null;
}
byte h;//hight index
byte l;//low index