How to convert base64 to base10 in PHP?

不羁岁月 提交于 2019-12-08 06:35:52

问题


I currently convert base10 numbers to base64 in PHP. but I haven't any idea for convert base64 to base10 in php!

How can I do it?

my algorithm for convert base10 to base64:

$rep = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','-','_');
$new = "";
while ($num>0) {
    $r = $num % 64;
    $new .= $rep[$r];
    $num = floor($num/64);
}

回答1:


Here is a Javascript way of doing it. Perhaps you can code one for php.

var ALPHABET = {'-': 62, '1': 53, '0': 52, '3': 55, '2': 54, '5': 57, '4': 56, '7': 59, '6': 58, '9': 61, '8': 60, 'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3, 'G': 6, 'F': 5, 'I': 8, 'H': 7, 'K': 10, 'J': 9, 'M': 12, 'L': 11, 'O': 14, 'N': 13, 'Q': 16, 'P': 15, 'S': 18, 'R': 17, 'U': 20, 'T': 19, 'W': 22, 'V': 21, 'Y': 24, 'X': 23, 'Z': 25, '_': 63, 'a': 26, 'c': 28, 'b': 27, 'e': 30, 'd': 29, 'g': 32, 'f': 31, 'i': 34, 'h': 33, 'k': 36, 'j': 35, 'm': 38, 'l': 37, 'o': 40, 'n': 39, 'q': 42, 'p': 41, 's': 44, 'r': 43, 'u': 46, 't': 45, 'w': 48, 'v': 47, 'y': 50, 'x': 49, 'z': 51};

function decode(s) {
    var n = 0;
    for(var i=0; i < s.length; i++) {
        var c = s.charAt(i);
        n = n * 64 + ALPHABET[c];
    }
    return n;
}

console.log(decode('EqfyK'));



回答2:


Here's the PHP version of the Javascript in Steve's answer:

function lengthen($id) {
    $alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';

    $number=0;
    foreach(str_split($id) as $letter) {
        $number=($number*64) + strpos($alphabet,$letter);
    }
    return $number;
}

Note that the alphabet is different than yours. The alphabet was based off of the answer here: Base10 to base64 url shortening




回答3:


If you are trying to write a PHP code for base conversion using numeric literals then that might not be possible.

PHP doesn't seem to actually deal with a hex literal (or octal for that matter). Apparently, when a hex is parsed, PHP internally converts the hex to decimal before used!

Here are a couple of scenarios that demonstrate this. (Check PHP: Integers - Manual to see notations used by PHP for numeric literals.)

Echoing

Numeric literals are resolved to their decimal equivalents before outputted.

// code       output

echo 26;      // 26
echo 0x1A;    // 26

Base Conversion

Using PHP's function base_convert() better demonstrates how PHP interprets hex to decimal before usage. (Check PHP: base_convert - Manual.)

With standard usage we convert the hex 0x1A to decimal and vice-versa.

echo base_convert('0x1A', 16, 10);       // 26
echo base_convert('26', 16, 10);         // 1a

Now for non-standard usage, decimal to hex works fine but not vice-versa.

echo base_convert(0x1A, 16, 10);         // 38
echo base_convert(26, 10, 16);           // 1a

Apparently, the hex 0x1A is internally converted to decimal 26 before used. This can be demonstrated by converting the hex '26' to decimal.

echo base_convert(0x1A, 16, 10);         // 38
echo base_convert(26, 16, 10);           // 38


来源:https://stackoverflow.com/questions/10954803/how-to-convert-base64-to-base10-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!