• java中如何将byte[]里面的数据转换成16进制字符串


    原文链接: http://zhidao.baidu.com/link?url=RmLDjr4PtP_oUE5J2pKNZSvlHt1K7HcCh4-03Y7VkXYhJ0kawg01CtKHZc2uBVxQsHgzl8pp60WBYULP6K15K_

    以下3个方法能将byte[]转化成16进制字符串,可以任选一个

    /* *
    * Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)
    *来转换成16进制字符串。
    * @param src byte[] data
    * @return hex string
    */

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

    public static String bytes2HexString(byte[] b) {
      String ret = "";
      for (int i = 0; i < b.length; i++) {
        String hex = Integer.toHexString(b[i] & 0xFF);
        if (hex.length() == 1) {
          hex = "0" + hex;
        }
        ret += hex;
      }
      return ret;
    }


    public static String toHex(byte[] buffer) {
      StringBuffer sb = new StringBuffer(buffer.length * 2);
      for (int i = 0; i < buffer.length; i++) {
        sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));
        sb.append(Character.forDigit(buffer[i] & 15, 16));
      }
      return sb.toString();
    }
  • 相关阅读:
    CodeForces 500C New Year Book Reading
    CodeForces 460B Little Dima and Equation 枚举
    CodeForces 451B Sort the Array
    【jquery】jQuery实现轮播图
    【IDEA】IDEA技巧记录
    【eclipse】日常使用eclipse记录
    【SSM】spring配置文件中读取配置文件的三种方式
    【Git】IDEA克隆和提交项目于码云
    semantic UI—表单验证
    【spring Data Jpa】JPA生成数据库表
  • 原文地址:https://www.cnblogs.com/huhu0013/p/4756917.html
Copyright © 2020-2023  润新知