0

How do you increment a hex value in java and keep the padding at the same time?

If you look at this:

Is there any sample Java code that does AES encryption exactly like this website?

You'll see a variable called "keyHex" in the correct answer which represents hex but in string format and padded by a bunch of zeros.

Then I want to increment "keyHex" by 1 and change it from:

"00000000000000000000000000000000"

to

"00000000000000000000000000000001"

I want to keep looping and adding 1 until I reach:

"00000000000000000000000000FFFFFF"

For obvious reasons I cannot do "keyHex++", so I would appreciate it if someone can point me in the right direction.

Community
  • 1
  • 1
user1068636
  • 1,871
  • 7
  • 33
  • 57

1 Answers1

3

Well what you should keep track of is the integer value of keyHex and then simply output it like this:

String.format("%033x", keyHex);

You could parse keyHex to an integer first if you need to as well.. This link may be more help for that: Java - parse and unsigned hex string into a signed long

Community
  • 1
  • 1
Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77
  • depending on how large these values will go you may need to use a different type than an int, either long or the BigInteger class. – Matt Wolfe Oct 27 '12 at 22:26