We have a very weird requirement to convert 11 digit hexadecimal number ( range from (A0000000001 ~ AFFFFFFFFFF) ) to 11 digit decimal number. And there should be 1:1 mapping between hexadecimal to integer. Is there a way to do this in Java ?
2 Answers
This is not possible, in Java or in any other language or system. This impossibility is a fundamental property of numbers and set theory.
Since the leading 0xA
does not change, what you actually have is a 10-hex digit value, or 40 bits worth of data.
This encompasses about 1.1 x 1012 possible values, so it is not possible to map this to an 11-digit decimal number, which by definition has only 1011 possible values.
Based on the comments I'm going to make this more explicit. Start with 0xA0000000000
and map that to decimal 00000000000
, then increment both by 1 to establish a one-to-one correspondence between members of the two sets and repeat:
0xA0000000000 -> 00000000000
0xA0000000001 -> 00000000001
0xA0000000002 -> 00000000002
...
0xA0000000010 -> 00000000016
...
0xA0000000100 -> 00000000256
...
0xA0000001000 -> 00000004096
...
0xA174876E7FF -> 99999999999
...
0xA174876E800 -> oops! overflow! too many digits
Put another way, the size of the set of all hex values between 0xA0000000000
and 0xAFFFFFFFFFF
is larger than the set of all decimal values between 00000000000
and 99999999999
.

- 85,615
- 20
- 155
- 190
-
He wants this mapped to 11 digits from 10 digits hex the A digit remains the same – johnny 5 Aug 30 '17 at 18:21
-
He explicitly said ***decimal*** output. You cannot map over a trillion distinct values to an 11-digit number. – Jim Garrison Aug 30 '17 at 18:22
-
yeah I'm just pointing out that he only doing this from a range and does not need all potential values. – johnny 5 Aug 30 '17 at 18:26
-
A0000000001 = 12094627905535 AFFFFFFFFFF = 10995116277761 Range Of Values = 1099511627774 #Of unique digits required 13 – johnny 5 Aug 30 '17 at 18:34
-
@JimGarrison As Johnny pointed that i don't want to map trillion distinct values to 11 digit number. All i want is 11 digit unique number from 10 digit HEX as mentioned above. – Neer1009 Aug 31 '17 at 02:55
String hexNumber = ...
int decimal = Integer.parseInt(hexNumber, 16);
Not sure if you need to display ten characters or not. If so, here is some code to convert it into a string and pad leading zeros.
String decNumber = Integer.toString(decimal);
public static String padLeadingZeros(String data, int requiredLength){
StringBuffer sb = new StringBuffer();
int numLeadingZeros = requiredLength - data.length();
for (int i=0; i < numLeadingZeros; i++){
sb.append("0");
}
sb.append(data);
return sb.toString();
}

- 80
- 5