10进制和64进制相互转换 php版

使用:表示10以及使用;表示11的原因是根据ascii的排列来选择的,这样要对64进制比较大小,直接比较就可以了。。。

<?php

function dec2s4($dec) {
$base = '0123456789:;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$result = '';

do {
$result = $base[$dec % 64] . $result;
$dec = intval($dec / 64);
} while ($dec != 0);

return $result;
}

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

for ($n = 0; $n < $len; $n++) {
$result *= 64;
$result += $base_map[$sixty_four{$n}];
}

return $result;
}

echo s42dec(dec2s4(0));
echo s42dec(dec2s4(10));
echo s42dec(dec2s4(63));
echo s42dec(dec2s4(64));
echo s42dec(dec2s4(111));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值