Convert hex to INT 32 and STRING Little Endian

kros365 :

First question:

Hex: F1620000

After convert hex to INT 32 i expect 253229, but i get -245235712. I've tried these methods:

Integer.parseUnsignedInt(value, 16));
(int)Long.parseLong(value, 16));
new BigInteger(value, 16).intValue());

How i can get correct value?

Second question:

Hex: 9785908D9B9885828020912E208D2E

After covert hex to STRING i can get this value:

\u0097\u0085\u0090\u008d\u009b\u0098\u0085\u0082\u0080 \u0091. \u008d.

How can I display this value correctly in json? (usning JSONObject).

StringBuilder result = new StringBuilder();
  for (int i = 0; i < value.length(); i += 2) {
    String str = value.substring(i, i + 2);
      result.append((char)Integer.parseInt(str, 16));
  }
Holger :

All your attempts are sufficient for parsing a hexadecimal string in an unsigned interpretation, but did not account for the “little endian” representation. You have to reverse the bytecode manually:

String value = "F1620000";
int i = Integer.reverseBytes(Integer.parseUnsignedInt(value, 16));
System.out.println(i);
25329

For your second task, the missing information was how to interpret the bytes to get to the character content. After searching a bit, the Codepage 866 seems to be the most plausible encoding:

String value = "9785908D9B9885828020912E208D2E";
byte[] bytes = new BigInteger(value, 16).toByteArray();
String result = new String(bytes, bytes[0]==0? 1: 0, value.length()/2, "CP866");
ЧЕРНЫШЕВА С. Н.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=420010&siteId=1