简体   繁体   English

在 java 中从十六进制转换为整数,反之亦然

[英]Convert from hex to int and vice versa in java

I have next method for converting to int from hex:我有下一个从十六进制转换为 int 的方法:

public static byte[] convertsHexStringToByteArray2(String hexString) {
    int hexLength = hexString.length() / 2;
    byte[] bytes = new byte[hexLength];
    for (int i = 0; i < hexLength; i++) {
        int hex = Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16);
        bytes[i] = (byte) hex;
    }

    return bytes;
}

when I use it for "80" hex string I get strange, not expected result:当我将它用于“80”十六进制字符串时,我会感到奇怪,不是预期的结果:

public static void main(String[] args) {
    System.out.println(Arrays.toString(convertsHexStringToByteArray2("80"))); // gives -128
    System.out.println(Integer.toHexString(-128));
    System.out.println(Integer.toHexString(128));
}

the output: output:

[-128]
ffffff80
80

I expect that "80" will be 128. What is wrong with my method?我预计“80”将是 128。我的方法有什么问题?

I have next method for converting to int from hex我有下一个从十六进制转换为 int 的方法

The method you posted converts a hex String to a byte array, and not to an int .您发布的方法将十六进制 String 转换为字节数组,而不是int That's why it is messing with its sign.这就是为什么它弄乱了它的标志。

Converting from hex to int is easy:从十六进制转换为整数很容易:

Integer.parseInt("80", 16)
$1 ==> 128

But if you want to get a byte array for further processing by just casting:但是,如果您想通过强制转换获得一个字节数组以进行进一步处理:

(byte) Integer.parseInt("80", 16)
$2 ==> -128

It "changes" its sign.它“改变”了它的符号。 For further information on primitives and signed variable types take a look at Primitive Data Types , where it says:有关原语和有符号变量类型的更多信息,请查看Primitive Data Types ,其中说:

The byte data type is an 8-bit signed two's complement integer.字节数据类型8 位有符号二进制补码 integer。 It has a minimum value of -128 and a maximum value of 127 (inclusive) .它的最小值为-128 ,最大值为127(含) The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.字节数据类型可用于将 memory 保存在大型 arrays 中,其中 memory 节省实际上很重要。 They can also be used in place of int where their limits help to clarify your code;它们也可以用来代替 int ,它们的限制有助于澄清您的代码; the fact that a variable's range is limited can serve as a form of documentation.变量的范围有限这一事实可以作为一种文档形式。

One could easily invert the sign by just increasing the value to convert:只需增加要转换的值,就可以轻松地反转符号:

(byte) Integer.parseInt("80", 16) & 0xFF
$3 ==> 128

That gets you a byte with the value you expect.这将为您提供一个具有您期望值的字节。 Technically that result isn't correct and you must to switch the sign again, if you want to get an int or a hex string back again.从技术上讲,这个结果是不正确的,如果你想再次获得一个 int 或一个十六进制字符串,你必须再次切换符号。 I'd suggest you to don't use a byte array if you only want to convert between hex and dec.如果您只想在十六进制和十进制之间转换,我建议您不要使用字节数组。

A byte in Java stores numbers from -128 to 127. 80 in hex is 128 as an integer, which is too large to be stored in a byte. Java 中的一个字节存储从 -128 到 127 的数字。十六进制的 80 是 128 作为 integer,它太大而无法存储在一个字节中。 So, the value wraps around.因此,价值环绕。 Use a different type to store your value (such as a short).使用不同的类型来存储您的值(例如 short)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM