php一般字符和16进制字符互相转换的函数
程序员文章站
2022-04-28 08:38:32
...
function SingleDecToHex($dec)
{
$tmp="";
$dec=$dec%16;
if($decreturn $tmp.$dec;
$arr=array("a","b","c","d","e","f");
return $tmp.$arr[$dec-10];
}
function SingleHexToDec($hex)
{
$v=Ord($hex);
if(47return $v-48;
if(96return $v-87;
}
function SetToHexString($str)
{
if(!$str)return false;
$tmp="";
for($i=0;$i{
$ord=Ord($str[$i]);
$tmp.=SingleDecToHex(($ord-$ord%16)/16);
$tmp.=SingleDecToHex($ord%16);
}
return $tmp;
}
function UnsetFromHexString($str)
{
if(!$str)return false;
$tmp="";
for($i=0;$i{
$tmp.=chr(SingleHexToDec(substr($str,$i,1))*16+SingleHexToDec(substr($str,$i+1,1)));
}
return $tmp;
}
?>
SetToHexString("大家好")==SetToHexString("大家好")?>
UnsetFromHexString(SetToHexString("大家好"))==UnsetFromHexString(SetToHexString("大家好"))?>
{
$tmp="";
$dec=$dec%16;
if($decreturn $tmp.$dec;
$arr=array("a","b","c","d","e","f");
return $tmp.$arr[$dec-10];
}
function SingleHexToDec($hex)
{
$v=Ord($hex);
if(47return $v-48;
if(96return $v-87;
}
function SetToHexString($str)
{
if(!$str)return false;
$tmp="";
for($i=0;$i
$ord=Ord($str[$i]);
$tmp.=SingleDecToHex(($ord-$ord%16)/16);
$tmp.=SingleDecToHex($ord%16);
}
return $tmp;
}
function UnsetFromHexString($str)
{
if(!$str)return false;
$tmp="";
for($i=0;$i
$tmp.=chr(SingleHexToDec(substr($str,$i,1))*16+SingleHexToDec(substr($str,$i+1,1)));
}
return $tmp;
}
?>
SetToHexString("大家好")==SetToHexString("大家好")?>
UnsetFromHexString(SetToHexString("大家好"))==UnsetFromHexString(SetToHexString("大家好"))?>
上一篇: Java多线程详解
下一篇: php正则表达式\w\d是什么?