Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert AlphaNumeric To Numeric And Should Be An Unique Number

I want to convert an alphanumeric(string) value to numeric(int). alphanumeric values like 'af45TR'. I wanted that numeric(int) value should be unique and it should not go out of the size of int like 64, 32 bits. How can i do that?.

like image 734
Sanganabasu Avatar asked Nov 17 '25 00:11

Sanganabasu


1 Answers

If you consider the string to be a number in base 62, you could write a function to perform a base conversion from base 62 to base 10. An example in PHP could be;

function base62toDec($n)
{
    $vals = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $vals = array_flip(str_split($vals));
    $out = 0;
    $len = strlen($n);
    for ($i = 0; $i < $len; $i++) {
        $c = $n[$len - ($i + 1)];
        $out += $vals[$c] * pow(62, $i);
    }
    return $out;
}

echo base62toDec('af45TR'); // outputs 9383949355
like image 180
Pudge601 Avatar answered Nov 19 '25 15:11

Pudge601



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!