字符串和byte数组的相互转化

关于byte[]数组转十六进制字符串:

public static String getHexString(byte[] b) throws Exception {
  String result = "";
  for (int i=0; i < b.length; i++) {
    result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  }
  return result;
}
 
public static byte[] getByteArray(String hexString) {
  return new BigInteger(hexString,16).toByteArray(); 
}

关于byte[]数组和String之间的相互转换还是比较简单的:

1 String str = "Hello";
2 byte[] srtbyte = str.getBytes();//当然你也可以设定编码方式
3 // byte[] 转 string
4 String res = new String(srtbyte);
5 System.out.println(res);

 


posted @ 2014-11-25 20:35  tomi_mint  阅读(633)  评论(0编辑  收藏  举报