0

i get android id

GB.AndroidId=CheckDataNull(Settings.Secure.getString(getApplicationContext().getContentResolver(),Settings.Secure.ANDROID_ID));

result is a8606c9f06af341f = this is hex value
i want to convert it to decimal value
result must be 12132816826403861535
converted online with this site https://www.rapidtables.com/convert/number/hex-to-decimal.html

i already tried this code

int decimal=Integer.parseInt(hex,16); 

but get an error

java.lang.NumberFormatException: Invalid int: "a8606c9f06af341f"

then i tried Converting Hexadecimal String to Decimal Integer it's also don't work

this code works but output is different
input = a8606c9f06af341f
output =859058534

public static int hex2decimal(String s) {
         String digits = "0123456789ABCDEF";
         s = s.toUpperCase();
         int val = 0;
         for (int i = 0; i < s.length(); i++) {
             char c = s.charAt(i);
             int d = digits.indexOf(c);
             val = 16*val + d;
         }
         return val;
     }

QUESTION : how can i get correct value ?
input=a8606c9f06af341f
output=12132816826403861535

Correct Answer is

BigInteger decimal = new BigInteger(hex , 16);
OXYGEN
  • 229
  • 2
  • 10
  • 1
    An int has a maximum of 32 bits, or 8 hex digits. Use a long. `Long.parseLong` – Nicolas Nov 13 '20 at 12:28
  • 1
    You might also need `Long.parseUnsignedLong` in this case. See also [Java converting int to hex and back again](https://stackoverflow.com/questions/12005424/java-converting-int-to-hex-and-back-again) – Nicolas Nov 13 '20 at 12:42
  • @Nicolas thank you for link, Correct is `BigInteger decimal = new BigInteger(hex , 16);` , but this `Long.parseUnsignedLong` gives wrong result – OXYGEN Nov 13 '20 at 14:49
  • 1
    That depends on the maximum bit width of your ID. Long will support up to 64 bits, otherwise you have to use BigInteger like you found. – Nicolas Nov 13 '20 at 14:50

0 Answers0