<?php
$value = $_REQUEST["val"];
$ctype = $_REQUEST["ctype"];
echo "输入十进制数字:<form method="post" action="index.php"><input type="text" name="val" value="".$value.""/>
<input type="radio" name="ctype" value="1036" checked> 10->36
<input type="radio" name="ctype" value="3610"> 36->10
<input type="submit" value="转换"/></form>";
if( (isset($value)==false) && (isset($ctype)==false)){
exit;
}
else {
if($ctype=="1036"){
$result=get_char($value);
} else if ($ctype=="3610"){
$result=get_num($value);
}
echo "转换结果:".$result."<br>";
echo "<img src="http://172.16.5.118/cets_web/doc_root/qrApi.php?val=".$value."&size=2"/>";
echo "<img src="http://172.16.5.118/cets_web/doc_root/qrApi.php?val=".$result."&size=2"/>";
}
/**
* @desc im:十进制数转换成三十六机制数
* @param (int)$num 十进制数
* return 返回:三十六进制数
*/
function get_char($num) {
$num = intval($num);
if ($num <= 0)
return false;
$charArr = 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");
$char = '';
do {
$key = ($num - 1) % 36;
$char= $charArr[$key] . $char;
$num = floor(($num - $key) / 36);
} while ($num > 0);
return $char;
}
/**
* @desc im:三十六进制数转换成十机制数
* @param (string)$char 三十六进制数
* return 返回:十进制数
*/
function get_num($char){
$array=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");
$len=strlen($char);
for($i=0;$i<$len;$i++){
$index=array_search($char[$i],$array);
$sum+=($index+1)*pow(36,$len-$i-1);
}
return $sum;
}
?>