11 Replies Latest reply on Nov 19, 2009 7:12 PM by 796440

# Problem with incrementing Hex from string

I am having problem incrementing a hex string below then convert it back to string.
Basically i just want to make ABCDE5AC7E112345 into ABCDE5AC7E112346. I get number format error below and not sure what i am doing wrong. some help appreciated!
``````String hex = "ABCDE5AC7E112345";
int value = Integer.parseInt(hex, 16);
value++;
String incHex = Integer.toHexString(value);``````
• ###### 1. Re: Problem with incrementing Hex from string
The hex number specified is much too big for an int.

• ###### 2. Re: Problem with incrementing Hex from string
same problem...
• ###### 3. Re: Problem with incrementing Hex from string
The parseXxx methods do not use twos complement.
``````int x = 0x80000001; // -2147483647
int y = Integer.parseInt("80000001", 16); // error
int z = Integer.parseInt("-7FFFFFFF", 16); // -2147483647``````
Edited by: jverd on Nov 19, 2009 10:01 AM
• ###### 4. Re: Problem with incrementing Hex from string
ok, i tested it out and it is indeed length problem. Long still too short and Double I wasnt sure how to get it to work. I guess i need to split the hex apart, increment one of them and check if it overflowed onto the second part?
like

12345FFFFFF - > if i split into 12345, FFFFFF and increment FFFFFF i need to check if the left most is 0 or not and different from original char, if it is then i increment the left side?
• ###### 5. Re: Problem with incrementing Hex from string
You could use BigInteger. And no, double will not work here.

Of course, it's not clear what you're trying to accomplish, so these suggestions are just guesses.

Edited by: jverd on Nov 19, 2009 10:14 AM
• ###### 6. Re: Problem with incrementing Hex from string
actually i want to decrease the hex value of the string by 1. I made a mistake on increment part.....
• ###### 7. Re: Problem with incrementing Hex from string
shaselai wrote:
actually i want to decrease the hex value of the string by 1. I made a mistake on increment part.....
Everything I've said still applies.
• ###### 8. Re: Problem with incrementing Hex from string
how do i use the BigInteger to do this? thanks
• ###### 9. Re: Problem with incrementing Hex from string
shaselai wrote:
how do i use the BigInteger to do this? thanks
Step 1. Read the docs, keeping an eye out for constructors and methods that have names, arguments, and return types that look like they may be useful for what you're trying to do. [http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html]

Step 2. Write code.

Step 3. Test code.

Step 4. Debug code.

If you get stuck, post again.
• ###### 10. Re: Problem with incrementing Hex from string
ok here's what i did....
``````               String hex = "ABCDEF127E1BBA9D";
BigInteger hexValue = new BigInteger(hex,16);
resultValue = hexValue.subtract(BigInteger.ONE);
String result = resultValue.toString(16);
System.err.println(hex);
System.err.println(result.toUpperCase());``````
and it works correctly for now...?

Edited by: shaselai on Nov 19, 2009 10:46 AM

Edited by: shaselai on Nov 19, 2009 10:48 AM
• ###### 11. Re: Problem with incrementing Hex from string
shaselai wrote:
and it works correctly for now...?
How do I know if it works correctly? You're the one running the code. I took a quick perv over the code and it looked all right to me, but the final arbiter is your own unit testing. Feed it various inputs--both valid and invalid--and see if behaves as you expect.