2个回答
展开全部
//汉字进制转换
//$string 传入的汉空穗字或其它字符
//$tobase 要转换的进制 范围 2~36 之间任意颂做取值
//$back 返回形式,0 转换成进制代码 ; 1 将进制转换成汉字
function ASCII($String,$tobase,$back){
if( $tobase > 1 && $tobase < 37 ){
if($back == 0){
return preg_replace('/(.)/es',"str_pad(base_convert(ord('\\1'),10,$tobase),8,'0',STR_PAD_LEFT)",$String);
}else{
return preg_replace('/(\w{8})/e',"chr(base_convert('\\1',$tobase,10))",$String);
}
}else{
return false;
}
}
//范例
$str = '野亏衡我爱你';
$en = ASCII($str,2,0); // 转换成2进制: 111001101000100010010001111001111000100010110001111001001011110110100000
echo ASCII($en,2,1); // 转换成汉字:我爱你
$en = ASCII($str,8,0); // 转换成8进制: 000003460000021000000221000003470000021000000261000003440000027500000240
echo ASCII($en,8,1); // 转换成汉字:我爱你
$en = ASCII($str,10,0); // 转换成10进制: 000002300000013600000145000002310000013600000177000002280000018900000160
echo ASCII($en,10,1); // 转换成汉字:我爱你
$en = ASCII($str,10,0); // 转换成10进制: 000002300000013600000145000002310000013600000177000002280000018900000160
echo ASCII($en,10,1); // 转换成汉字:我爱你
$en = ASCII($str,16,0); // 转换成10进制: 000000e60000008800000091000000e700000088000000b1000000e4000000bd000000a0
echo ASCII($en,16,1); // 转换成汉字:我爱你
//$string 传入的汉空穗字或其它字符
//$tobase 要转换的进制 范围 2~36 之间任意颂做取值
//$back 返回形式,0 转换成进制代码 ; 1 将进制转换成汉字
function ASCII($String,$tobase,$back){
if( $tobase > 1 && $tobase < 37 ){
if($back == 0){
return preg_replace('/(.)/es',"str_pad(base_convert(ord('\\1'),10,$tobase),8,'0',STR_PAD_LEFT)",$String);
}else{
return preg_replace('/(\w{8})/e',"chr(base_convert('\\1',$tobase,10))",$String);
}
}else{
return false;
}
}
//范例
$str = '野亏衡我爱你';
$en = ASCII($str,2,0); // 转换成2进制: 111001101000100010010001111001111000100010110001111001001011110110100000
echo ASCII($en,2,1); // 转换成汉字:我爱你
$en = ASCII($str,8,0); // 转换成8进制: 000003460000021000000221000003470000021000000261000003440000027500000240
echo ASCII($en,8,1); // 转换成汉字:我爱你
$en = ASCII($str,10,0); // 转换成10进制: 000002300000013600000145000002310000013600000177000002280000018900000160
echo ASCII($en,10,1); // 转换成汉字:我爱你
$en = ASCII($str,10,0); // 转换成10进制: 000002300000013600000145000002310000013600000177000002280000018900000160
echo ASCII($en,10,1); // 转换成汉字:我爱你
$en = ASCII($str,16,0); // 转换成10进制: 000000e60000008800000091000000e700000088000000b1000000e4000000bd000000a0
echo ASCII($en,16,1); // 转换成汉字:我爱你
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询