Is there any java utility to convert string to hex value (integer) ?
Asked
Active
Viewed 1.6k times
5
-
Do you want to turn a string such as "abc" into the hex value of each character, or are you trying to turn something like "0x3df" into a numerical value? – Pesto Jul 08 '09 at 12:29
-
Do you mean a string that represents a number? Or an arbitrary text string? – jwoolard Jul 08 '09 at 12:29
-
2Your question is too ambiguous. Give some examples of your desired input and output. – Stu Thompson Jul 08 '09 at 12:30
-
Maybe look at this question: https://stackoverflow.com/q/923863/16632604 – Flimtix Jun 02 '22 at 12:10
4 Answers
14
When you have a string starting with 0x or #
Integer.decode(hexStr);
is the goal
Or
Integer.parseInt(hexString, 16);

Markus Lausberg
- 12,177
- 6
- 40
- 66
-
This is what i need, thanx.. Integer i = Integer.decode("0xA"); System.out.println(i); output : 10 – penguru Jul 08 '09 at 12:48
4
Is this what you are looking for?
Integer.toHexString(Integer.parseInt(String));

AlbertoPL
- 11,479
- 5
- 49
- 73
3
Your question is a little ambiguous, I think.
If you have a hex string (e.g. "ab10"), then you can use
int i = Integer.valueOf(s, 16).intValue();

Brian Agnew
- 268,207
- 37
- 334
- 440