Java – Convert hex to small end
•
Android
What is the formula for converting the value of a text field from hexadecimal to small end?
Input example: 5a109061
Output example: 1636831322
resolvent:
>Get the value from EditText as a string. > use integer. ParseInt (...) and Radix 16 to parse the string value into hexadecimal. > use ByteBuffer (simpler) or shift (faster) to flip the byte order of int
For example:
String hex = "5A109061"; // mEditText.getText().toString()
// Parse hex to int
int value = Integer.parseInt(hex, 16);
// Flip byte order using ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.asIntBuffer().put(value);
buffer.order(ByteOrder.LITTLE_ENDIAN);
int flipped = buffer.asIntBuffer().get();
System.out.println("hex: 0x" + hex);
System.out.println("flipped: " + flipped);
Output:
hex: 0x5A109061
flipped: 1636831322
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码