I have a hex String 240300000800000000000000004. I want to add integer in this hex code and get back hex code in java.
As java does not support this muck length of value, so how do we do this?
Thanks Abhishek
String str = "240300000800000000000000004";
BigInteger bi = new BigInteger(str, 16);
Integer ii = 10; // Integer to add
bi = bi.add(new BigInteger(ii.toString()));
System.out.println(bi.toString(16)); // 24030000080000000000000000e
BigInteger from a hex string by specifying the base 16 to convert from.Use BigInteger in Java
That will satisfy your requirements.
Visit the following tutorial to learn BigInteger in Java : TutorialPoint
The easiest solution is the already posted one of using BigInteger. However, you can also process the addition in chunks such that the result and any carry will fit in a long. Start at the right hand end with carry-in zero. Take a group of up to 15 hex digits, convert to long, do the addition including any carry-in from the addition to the right of this chunk, store the 15 least significant digits of the result into the output string, and remember the most significant digit to use as the carry-out from this addition, the carry-in to the next addition to the left.
BigInteger bigint = new BigInteger("240300000800000000000000004" ,16);
System.out.println(bigint);
BigInteger s = new BigInteger("8");
bigint = bigint.add(s);
System.out.println(bigint);
0x