Java reads hexadecimal values into arrays of type int
•
Java
I have a file that contains integers in hexadecimal
I know you can say int i = 0x
But when reading I get the wrong value, I can't do this?
Thank you in advance!
Solution
You may want to pass integer parseInt(yourHexValue,16).
Example:
// Your reader BufferedReader sr = new BufferedReader(new StringReader("cafe\nBABE")); // Fill your int-array String hexString1 = sr.readLine(); String hexString2 = sr.readLine(); int[] intArray = new int[2]; intArray[0] = Integer.parseInt(hexString1,16); intArray[1] = Integer.parseInt(hexString2,16); // Print result (in dec and hex) System.out.println(intArray[0] + " = " + Integer.toHexString(intArray[0])); System.out.println(intArray[1] + " = " + Integer.toHexString(intArray[1]));
Output:
51966 = cafe 47806 = babe
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
二维码