Convert Google Contact ID to Hex to use in URL

0

Google Contacts now (Jan 2019) issues a long (19 digit) decimal number id for each contact that you create.

Unfortunately, as discussed in this question the ID cannot be put into a URL to view the contact easily, however if you convert this decimal number to Hex it can be put into the URL.

So the question is, how to convert c2913347583522826972 to 286E4A310F1EEADC

When I use the Decimal to Hex converter here it gives me 286E4A310F1EEADC if I drop the c (2nd function below is a version of the sites code, but it does use PHP too maybe)

However trying the following functions in Javascript give me mixed results

The first one is from this stack question which is the closest, just 2 digits off

function decimalToHexString(number)
{
number = parseFloat(number);
  if (number < 0)
  {
    number = 0xFFFFFFFF + number + 1;
  }

  return number.toString(16);
}

console.log(decimalToHexString('2913347583522826972'));
//output 286e4a310f1eea00

function convertDec(inp,outp) {
    var pd = '';
    
    var output ;
    var input = inp;
    
    for (i=0; i < input.length; i++) {
        var e=input[i].charCodeAt(0);var s = "";
    output+= e + pd;
}
return output;
}
//return 50574951515255535651535050565054575550

Love to know your thoughts on improving this process

javascript
python
hex
decimal
asked on Stack Overflow Jan 29, 2019 by Craig Lambie

1 Answer

1

It seems like the limit of digit size. You have to use arrays if you need to convert bigger digits.

You can use hex2dec npm package to convert between hex and dec.

>> converter.decToHex("2913347583522826972", { prefix: false }
//286e4a310f1eeadc

Js example

On python side, you can simply do

dec = 2913347583522826972
// Python implicitly handles prefix
hexa =  hex(dec)
print dec == int(hexa, 16)
// True

Python example

For more take a look at the following gist https://gist.github.com/agirorn/0e740d012b620968225de58859ccef5c

answered on Stack Overflow Jan 29, 2019 by Talha Junaid • edited Jan 30, 2019 by Talha Junaid

User contributions licensed under CC BY-SA 3.0