将十六进制编码的整数字符串转换为Java中的整数数组

huangapple go评论64阅读模式
英文:

Convert string of hex-encoded integers to int array in Java

问题

使用Java正确将十六进制字符串转换为整数的方法如下:

String s = "c4d2"; // 十六进制字符串
int n = Integer.parseInt(s, 16); // 使用十六进制解析
System.out.println(s + " -> " + n);

这会将十六进制字符串 "c4d2" 转换为整数 53956,与你所期望的结果一致。

请注意,与Python不同,Java的整数解析方法要求输入的字符串必须以正确的十六进制格式表示,并且不需要额外的字节顺序或二进制转换步骤。

英文:

For example, we have a string "c4d2", known it is a little-endian coded 16-bit int 53956. (It is NOT about conversion "c4d2" to int[] {0xc4, 0xd2}, but about coversion of "c4d2" to integer 53956). How to convert the string to int use Java?

Use python I do

s = b'\xc4\xd2'
n = int.from_bytes(s, "little")
print(f"{s} -> {n}")

output is right

b'\xc4\xd2' -> 53956

Use Java I do

String s = "c4d2"; // 53956
byte[] b = HexFormat.of().parseHex(s);
int n = ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort();
System.out.println(s + " -> " + Arrays.toString(b) + " -> " + n);

and got

c4d2 -> [-60, -46] -> -11580

that is wrong. But if I do conversion through binary string as

String s2 = Integer.toBinaryString(0xd2) + Integer.toBinaryString(0xc4);
int n2 = Integer.parseInt(s2, 2);
System.out.println(s + " -> " + s2 + " -> " + n2);

I got

c4d2 -> 1101001011000100 -> 53956

the result is right, but the method seems stange.

How to properly convert hex-string to int?

答案1

得分: 1

Java的short是有符号的,因此其最大值是32767,你发生了溢出。你可以使用Short.toUnsignedInt()

String s = "c4d2"; // 53956
byte[] b = HexFormat.of().parseHex(s);
int n = Short.toUnsignedInt(ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort());
System.out.println(s + " -> " + Arrays.toString(b) + " -> " + n);

输出结果为:

c4d2 -> [-60, -46] -> 53956
英文:

Java's short is signed, so its max value is 32767 and you got overflow. You can use Short.toUnsignedInt():

    String s = "c4d2"; // 53956
    byte[] b = HexFormat.of().parseHex(s);
    int n = Short.toUnsignedInt(ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort());
    System.out.println(s + " -> " + Arrays.toString(b) + " -> " + n);

prints

> c4d2 -> [-60, -46] -> 53956

答案2

得分: 1

一种方法是反转这些值,然后使用Integer#parseInt方法,并指定基数为16。

String string = "c4d2";
StringBuilder reversed = new StringBuilder();
for (int index = string.length() - 1; index >= 0; index -= 2)
    reversed.append(string, index - 1, index + 1);
int number = Integer.parseInt(reversed.toString(), 16);

输出,对于_number_。

53956
英文:

One approach would be to reverse the values, and then use the Integer#parseInt method, specifying a 16 for the radix.

String string = "c4d2";
StringBuilder reversed = new StringBuilder();
for (int index = string.length() - 1; index >= 0; index -= 2)
    reversed.append(string, index - 1, index + 1);
int number = Integer.parseInt(reversed.toString(), 16);

Output, for number.

53956

huangapple
  • 本文由 发表于 2023年6月29日 02:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定