Java代码将字节转换为十六进制 [英] Java code To convert byte to Hexadecimal

查看:1241
本文介绍了Java代码将字节转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组。
我希望将该数组的每个字节字符串转换为相应的十六进制值。

I have an array of bytes. I want each byte String of that array to be converted to its corresponding hexadecimal values.

Java中是否有任何函数将字节数组转换为十六进制数?

Is there any function in Java to convert a byte array to Hexadecimal ?

推荐答案

    byte[] bytes = {-1, 0, 1, 2, 3 };
    StringBuilder sb = new StringBuilder();
    for (byte b : bytes) {
        sb.append(String.format("%02X ", b));
    }
    System.out.println(sb.toString());
    // prints "FF 00 01 02 03 "



参见




  • java.util.Formatter 语法


    • %[flags] [width]转换


      • 标记'0' - 结果将为零填充

      • 宽度 2

      • 转换'X' - 结果格式为十六进制整数,大写

      • See also

        • java.util.Formatter syntax
          • %[flags][width]conversion
            • Flag '0' - The result will be zero-padded
            • Width 2
            • Conversion 'X' - The result is formatted as a hexadecimal integer, uppercase
            • 查看问题的文本,这也可能是要求:

              Looking at the text of the question, it's also possible that this is what is requested:

                  String[] arr = {"-1", "0", "10", "20" };
                  for (int i = 0; i < arr.length; i++) {
                      arr[i] = String.format("%02x", Byte.parseByte(arr[i]));
                  }
                  System.out.println(java.util.Arrays.toString(arr));
                  // prints "[ff, 00, 0a, 14]"
              






              这里的几个答案使用 Integer.toHexString(int)的 ;这是可行的,但有一些警告。由于参数是 int ,因此对 byte 参数执行扩展的原始转换,该参数涉及符号扩展。 / p>


              Several answers here uses Integer.toHexString(int); this is doable, but with some caveats. Since the parameter is an int, a widening primitive conversion is performed to the byte argument, which involves sign extension.

                  byte b = -1;
                  System.out.println(Integer.toHexString(b));
                  // prints "ffffffff"
              

              8位字节,用Java签名,符号扩展为32位 int 。要有效撤消此符号扩展,可以使用 0xFF 屏蔽字节

              The 8-bit byte, which is signed in Java, is sign-extended to a 32-bit int. To effectively undo this sign extension, one can mask the byte with 0xFF.

                  byte b = -1;
                  System.out.println(Integer.toHexString(b & 0xFF));
                  // prints "ff"
              

              使用 toHexString的另一个问题是它没有用零填充:

              Another issue with using toHexString is that it doesn't pad with zeroes:

                  byte b = 10;
                  System.out.println(Integer.toHexString(b & 0xFF));
                  // prints "a"
              

              两个因素组合应该是 String.format 解决方案更优先。

              Both factors combined should make the String.format solution more preferrable.


              • JLS 4.2.1积分类型和值


                • 对于字节,来自 -128 127 ,包括

                • JLS 4.2.1 Integral Types and Values
                  • For byte, from -128 to 127, inclusive

                  这篇关于Java代码将字节转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆