java如何将0x00, 0x26, 0xA6, 0x54, 0x4F, 0xF2字节数组转换为,00:26:A6:54:4F:F2格式的字符串

解决方案 »

  1.   

    byte[] a = { 0x00, 0x26, (byte) 0xA6, 0x54, 0x4F, (byte) 0xF2 };
    String result = "";
    for (int i = 0; i < a.length - 1; i++)
    result = result + Integer.toHexString(a[i] & 0xff) + ":";//与上0xff转成无符号数,然后转成十六进制字符串
    result += Integer.toHexString(a[a.length - 1] & 0xff);
    result = result.toUpperCase();//转换为大写
    System.out.println(result);
      

  2.   

    String.format("%02X:%02X:%02X:%02X:%02X:%02X", 0x00, 0x26, 0xA6, 0x54, 0x4F, 0xF2);
      

  3.   

    我表示丢人了。。
    借用楼上方法,改过
    byte[] a = { (byte) 0x00, (byte) 0x26, (byte) 0xA6, (byte) 0x54,
    (byte) 0x4F, (byte) 0xF2 };
    String result = "";
    for (int i = 0; i < a.length; i++) {
    if (i == 0)
    result += String.format("%02X", a[i]);
    else
    result += String.format(":%02X", a[i]);
    }
    System.out.println(result);