1. Introduction
1.导言
Converting a hexadecimal (Hex) string into an integer is a frequent task during programming, particularly when handling data types that use hexadecimal notations.
将十六进制(Hex)字符串转换为整数是编程过程中经常遇到的任务,尤其是在处理使用十六进制符号的数据类型时。
In this tutorial, we’ll dive into various approaches to converting a Hex String into an int in Java.
在本教程中,我们将深入探讨在 Java 中将十六进制 String 转换为 int 的各种方法。
2. Understanding Hexadecimal Representation
2.了解十六进制表示法
Hexadecimal employs base-16, resulting in each digit that could take on 16 possible values from zero through nine, followed by (A) through (F):
十六进制采用基 16,因此每个数字可以有 16 个可能的值,从 0 到 9,后面是 (A) 到 (F):
Let’s also note that in most cases, hexadecimal strings begin with “0x” to denote its base.
请注意,在大多数情况下,十六进制字符串以”0x“开头,表示其基数。
3. Using Integer.parseInt()
3.使用 Integer.parseInt()
The easiest way of converting a hex string to an integer in Java is via the Integer.parseInt() method. It converts a string into an integer, assuming the base in which it was written. For us, the base is 16:
在 Java 中将十六进制字符串转换为整数的最简单方法是使用 Integer.parseInt() 方法。它可以将字符串转换为整数,前提是字符串的基数。对于我们来说,基数是 16:
@Test
public void givenValidHexString_whenUsingParseInt_thenExpectCorrectDecimalValue() {
String hexString = "0x00FF00";
int expectedDecimalValue = 65280;
int decimalValue = Integer.parseInt(hexString.substring(2), 16);
assertEquals(expectedDecimalValue, decimalValue);
}
In the above code, the hexadecimal string “0x00FF00” is converted to its corresponding decimal value of 65280 using Integer.parseInt, and the test asserts that the result matches the expected decimal value. Note that we use the substring(2) method to remove the “ox” part from the hexString.
在上述代码中,我们使用 Integer.parseInt 将十六进制字符串”0x00FF00“转换为相应的十进制值 65280,并断言测试结果与预期的十进制值一致。请注意,我们使用 substring(2) 方法删除了 hexString 中的”ox“部分。
4. Using BigInteger
4.使用BigInteger</em
For more flexibility when working with very large or unsigned hexadecimal values, we can consider using a BigInteger. It operates on arbitrary precision integers and can, therefore, be used in myriad contexts.
在处理非常大或无符号十六进制值时,为了获得更大的灵活性,我们可以考虑使用 BigInteger 。它可对任意精度的整数进行操作,因此可用于各种情况。
Here’s how we can convert a hex string to a BigInteger and then extract the integer value:
下面介绍如何将十六进制字符串转换为 BigInteger 并提取整数值:
@Test
public void givenValidHexString_whenUsingBigInteger_thenExpectCorrectDecimalValue() {
String hexString = "0x00FF00";
int expectedDecimalValue = 65280;
BigInteger bigIntegerValue = new BigInteger(hexString.substring(2), 16);
int decimalValue = bigIntegerValue.intValue();
assertEquals(expectedDecimalValue, decimalValue);
}
5. Using Integer.decode()
5.使用 Integer.decode()
Another way for changing a Hex string into an integer is provided by the Integer.decode() method. This approach deals with hexadecimal as well as decimal strings.
另一种将十六进制字符串转换为整数的方法是 Integer.decode() 方法。这种方法既可处理十六进制字符串,也可处理十进制字符串。
Here, we use Integer.decode() without stating the base as it is determined from a string itself:
在这里,我们使用 Integer.decode() 而不说明基数,因为基数是由字符串本身决定的:
@Test
public void givenValidHexString_whenUsingIntegerDecode_thenExpectCorrectDecimalValue() {
String hexString = "0x00FF00";
int expectedDecimalValue = 65280;
int decimalValue = Integer.decode(hexString);
assertEquals(expectedDecimalValue, decimalValue);
}
Because the Integer.decode() method can handle the “0x” prefix in the string, we don’t need to manually remove it using substring(2) as we did in the previous approaches.
由于Integer.decode()方法可以处理字符串中的”0x“前缀,因此我们无需像前面的方法那样使用substring(2)手动删除该前缀。
6. Conclusion
6.结论
In conclusion, we discussed the significance of hexadecimal representation and delved into three distinct approaches: Integer.parseInt() for a straightforward conversion, BigInteger for handling large or unsigned values, and Integer.decode() for versatility in handling both hexadecimal and decimal strings, including the “0x” prefix.
最后,我们讨论了十六进制表示法的意义,并深入探讨了三种不同的方法:Integer.parseInt()用于直接转换,BigInteger用于处理大数值或无符号数值,Integer.decode()用于同时处理十六进制和十进制字符串(包括”0x“前缀)的多功能性。
As always, the complete code samples for this article can be found over on GitHub.
与往常一样,本文的完整代码示例可在 GitHub 上找到。