源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Java中字符串与byte数组之间的相互转换

  • 时间:2022-05-17 22:13 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Java中字符串与byte数组之间的相互转换
[b]前言[/b] Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成[code]byte[/code]数组型,java在字符与数组转换也是非常方便的。下面跟我一起来了解一下字符串与[code]byte[/code]之间转换的原理 [b]原理[/b] 我们都知道,在Java里[code]byte[/code]类型是占用1个字节,即8位的,而16进制的字符占用4位,所以每个[code]byte[/code]可以用两个字符来表示,反之亦然。 [b]举个例子[/b]
byte = 123
用二进制表示:[code]0111 1011 [/code] 每4位用字符表示: [code]7 b[/code] 是的,原理就这么简单,接下来用代码实现: [b]byte[] 转16进制字符串[/b] [b]方法一[/b] 思路:先把[code]byte[] [/code]转换维[code]char[][/code] ,再把[code]char[][/code] 转换为字符串
 public static String bytes2Hex(byte[] src) {
  if (src == null || src.length <= 0) { 
   return null; 
  } 

  char[] res = new char[src.length * 2]; // 每个byte对应两个字符
  final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  for (int i = 0, j = 0; i < src.length; i++) {
   res[j++] = hexDigits[src[i] >> 4 & 0x0f]; // 先存byte的高4位
   res[j++] = hexDigits[src[i] & 0x0f]; // 再存byte的低4位
  }

  return new String(res);
 }
[b]方法二[/b] 思路:先把[code]byte[/code]转换为[code]int[/code]类型,再转换为字符串
 public static String bytesToHex(byte[] src){ 
  if (src == null || src.length <= 0) { 
   return null; 
  } 

  StringBuilder stringBuilder = new StringBuilder("");   
  for (int i = 0; i < src.length; i++) { 
   // 之所以用byte和0xff相与,是因为int是32位,与0xff相与后就舍弃前面的24位,只保留后8位
   String str = Integer.toHexString(src[i] & 0xff); 
   if (str.length() < 2) { // 不足两位要补0
    stringBuilder.append(0); 
   } 
   stringBuilder.append(str); 
  } 
  return stringBuilder.toString(); 
 }
[b]16进制字符串转byte[][/b] 思路:先把字符串转换为[code]char[] [/code],再转换为[code]byte[][/code]
 public static byte[] hexToBytes(String hexString) { 
  if (hexString == null || hexString.equals("")) { 
   return null; 
  } 

  int length = hexString.length() / 2; 
  char[] hexChars = hexString.toCharArray(); 
  byte[] bytes = new byte[length]; 
  String hexDigits = "0123456789abcdef";
  for (int i = 0; i < length; i++) { 
   int pos = i * 2; // 两个字符对应一个byte
   int h = hexDigits.indexOf(hexChars[pos]) << 4; // 注1
   int l = hexDigits.indexOf(hexChars[pos + 1]); // 注2
   if(h == -1 || l == -1) { // 非16进制字符
    return null;
   }
   bytes[i] = (byte) (h | l); 
  } 
  return bytes; 
 }
注:注1得到[code]xxxx0000[/code],注2得到[code]0000xxxx[/code],相或就把两个字符转换为一个[code]byte[/code]了。 [b]再举个例子[/b] [b]md5加密[/b]
 public static String getMd5ByFile(File file) {
  String ret= null;
  FileInputStream fis = null;
  try {
   fis = new FileInputStream(file);
   MessageDigest md = MessageDigest.getInstance("MD5");
   byte[] buffer = new byte[1024];
   int len;
   while((len = fis.read(buffer)) > 0) {
    md.update(buffer, 0, len);
   }
   ret = bytes2Hex(md.digest()); // 把md5加密后的byte[]转换为字符串
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if(fis != null) {
    try {
     fis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }

  return ret;
 }
[b]总结[/b] 好了,应该懂了吧,其实并不难的。以上就是这篇文章的全部内容了,希望这篇文章的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部