0

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 ?

5
  • Do you have a hex value, or do you have a bunch of 1's and 0's? Commented May 8, 2014 at 17:16
  • Actual hex value of C9E5249A. Commented May 8, 2014 at 17:22
  • What is the numeric value in C which you get from this hex value? Commented May 8, 2014 at 17:27
  • This has nothing at all to do with C. Even if you think so. Commented 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 ? Commented May 8, 2014 at 19:16

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

6 Comments

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)
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.
@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.
@Sergiu Then call Long.parseLong in the same way.
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.
|
1

try this

Long.parseLong("C9E5249A", 16);

C9E5249A is too big for Integer.parseInt which parses the string as a signed decimal integer, see API

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.