I have this hex value: C9E5249A which is representing a Twos Complement signed 32-bit integer representation in C. How can I get its Java counterpart ?
-
Do you have a hex value, or do you have a bunch of 1's and 0's?Eric J.– Eric J.2014-05-08 17:16:34 +00:00Commented May 8, 2014 at 17:16
-
Actual hex value of C9E5249A.Sergiu– Sergiu2014-05-08 17:22:30 +00:00Commented May 8, 2014 at 17:22
-
What is the numeric value in C which you get from this hex value?peter.petrov– peter.petrov2014-05-08 17:27:25 +00:00Commented May 8, 2014 at 17:27
-
This has nothing at all to do with C. Even if you think so.Deduplicator– Deduplicator2014-05-08 17:30:16 +00:00Commented May 8, 2014 at 17:30
-
I found this site: rapidtables.com/convert/number/hex-to-decimal.htm which for the input C9E5249A gives Hex = C9E5249A Decimal= 12×16⁷+9×16⁶+14×16⁵+5×16⁴+2×16³+4×16²+9×16¹+10×16⁰ = 3387237530 Signed decimal = -907729766 Binary = 11001001111001010010010010011010 My question would now be ... how can I also get that signed decimal ?Sergiu– Sergiu2014-05-08 19:16:26 +00:00Commented May 8, 2014 at 19:16
Add a comment
|
2 Answers
You can just call this:
Integer.parseInt("C9E5249A", 16)
or actually ...
Long.parseLong("C9E5249A", 16)
(because as others noted this hex value you have is too large for the int type).
6 Comments
Sergiu
I tried that ... it gives me this: Exception in thread "main" java.lang.NumberFormatException: For input string: "C9E5249A" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:495) at HelloWorld.main(HelloWorld.java:4)
Eric J.
The mention of 2's complement leads me to believe he may really have 1's and 0's rather than the actual hex value in the Java program.
Alexis C.
@Sergiu Because the "integer" represented by the hex value cannot fit in an integer. You can try
Long.parseLong(hex, 16), however I'm not sure what you really want to achieve.peter.petrov
@Sergiu Then call Long.parseLong in the same way.
Sergiu
I have to parse some raw data ... and the documentation I got is written for C programmers and I have to write the program in Java. Long.parseLong(hex, 16) will work but will not yield the correct value ... in the examples I have this should have been a negative value.
|