栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java byte数组与16进制间相互转换的示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java byte数组与16进制间相互转换的示例

1.准备工作

import java.util.Arrays;


public class ByteUtils {

  // 16进制字符
  private static final char[] HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}

2.byte类型数组转化成16进制字符串

  方法一


public static String toHexString(byte[] bytes) {
  StringBuilder sb = new StringBuilder();
  int num;
  for (byte b : bytes) {
    num = b < 0 ? 256 + b : b;
    sb.append(HEX_CHAR[num / 16]).append(HEX_CHAR[num % 16]);
  }
  return sb.toString();
}

  方法二


public static String toHexString2(byte[] bytes) {
  // 一个byte为8位,可用两个十六进制位表示
  char[] buf = new char[bytes.length * 2];
  int a = 0;
  int index = 0;
  // 使用除与取余进行转换
  for (byte b : bytes) {
    if (b < 0)
      a = 256 + b;
    else
      a = b;

    // 偶数位用商表示
    buf[index++] = HEX_CHAR[a / 16];
    // 奇数位用余数表示
    buf[index++] = HEX_CHAR[a % 16];
  }
  // char[]-->String
  return new String(buf);
}

  方法三


public static String toHexString3(byte[] bytes) {
  char[] buf = new char[bytes.length * 2];
  int index = 0;
  // 利用位运算进行转换,可以看作方法二的变型
  for (byte b : bytes) {
    buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
    buf[index++] = HEX_CHAR[b & 0xf];
  }

  return new String(buf);
}

  方法四


public static String toHexString4(byte[] bytes) {
  StringBuilder sb = new StringBuilder(bytes.length * 2);
  // 使用String的format方法进行转换
  for (byte b : bytes) {
    sb.append(String.format("%02x", new Integer(b & 0xff)));
  }

  return sb.toString();
}

  方法五


private static String bytesToHexString(byte[] src) {
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < src.length; i++) {
    int v = src[i] & 0xFF;
    String hv = Integer.toHexString(v);
    if (hv.length() < 2) {
      sb.append(0);
    }
    sb.append(hv);
  }
  return sb.toString();
}  

3.16进制字符串转换为byte[]

  方法一


public static byte[] fromHexString(String hexString) {
  if (null == hexString || "".equals(hexString.trim())) {
    return new byte[0];
  }

  byte[] bytes = new byte[hexString.length() / 2];
  // 16进制字符串
  String hex;
  for (int i = 0; i < hexString.length() / 2; i++) {
    // 每次截取2位
    hex = hexString.substring(i * 2, i * 2 + 2);
    // 16进制-->十进制
    bytes[i] = (byte) Integer.parseInt(hex, 16);
  }

  return bytes;
}

  方法二


public static byte[] fromHex(String hexStr) {
  if (hexStr.length() < 1)
    return null;
  byte[] result = new byte[hexStr.length() / 2];
  for (int i = 0; i < hexStr.length() / 2; i++) {
    int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
    int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
    result[i] = (byte) (high * 16 + low);
  }
  return result;
}

  方法三:

public static byte[] toByteArray(String data) {
    if (data == null) {
        return new byte[] {};
    }
    if (data.length() == 0) {
        return new byte[] {};
    }
    while (data.length() < 2) {
        data = "0" + data;
    }
    if (data.substring(0, 2).toLowerCase().equals("0x")) {
        data = data.substring(2);
    }
    if (data.length() % 2 == 1) {
        data = "0" + data;
    }
    data = data.toUpperCase();
    byte[] bytes = new byte[data.length() / 2];
    String hexString = "0123456789ABCDEF";
    for (int i = 0; i < bytes.length; i++) {
        int byteConv = hexString.indexOf(data.charAt(i * 2)) * 0x10;
        byteConv += hexString.indexOf(data.charAt(i * 2 + 1));
        bytes[i] = (byte) (byteConv & 0xFF);
    }
    return bytes;
}  

4.测试

public static void main(String[] args) throws Exception {
  String json = "{"name":"Marydon","website":"http://www.cnblogs.com/Marydon20170307"}";
  byte[] bytes = json.getBytes("utf-8");
  System.out.println("字节数组为:" + Arrays.toString(bytes));
  System.out.println("byte数组转16进制之方法一:" + toHexString(bytes));
  System.out.println("byte数组转16进制之方法二:" + ByteUtils.toHexString2(bytes));
  System.out.println("byte数组转16进制之方法三:" + ByteUtils.toHexString3(bytes));
  System.out.println("byte数组转16进制之方法四:" + ByteUtils.toHexString4(bytes));
  System.out.println("==================================");
  String str = "7b226e616d65223a224d617279646f6e222c2277656273697465223a22687474703a2f2f7777772e636e626c6f67732e636f6d2f4d617279646f6e3230313730333037227d";
  System.out.println("转换后的字节数组:" + Arrays.toString(fromHexString(str)));
  System.out.println(new String(fromHexString(str), "utf-8"));
}

补充

  1B=8b,也就是1byte=8bit;

  1KB=1024B;

  1MB=1024KB;

  1GB=1024MB;

  1TB=1024GB

  bit是计算机最小的存储单元,只能存储0和1,是Binary digit(二进制数位)的缩写,意为“位”或“比特”,也就是二进制。

以上就是java byte数组与16进制间相互转换的示例的详细内容,更多关于java byte数组与16进制间的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130910.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号