PHP 字符串和十六进制互转
<?php
class Test{
/**
*字符串转十六进制函数
*@pream string $str='abc';
*/
public function strToHex($str){
$hex="";
for($i=0;$i<strlen($str);$i++)
$hex.=dechex(ord($str[$i]));
$hex=strtoupper($hex);
return $hex;
}
/**
*十六进制转字符串函数
*@pream string $hex='616263';
*/
public function hexToStr($hex){
$str="";
for($i=0;$i<strlen($hex)-1;$i+=2)
$str.=chr(hexdec($hex[$i].$hex[$i+1]));
return $str;
}
}
<span style="white-space:pre"> </span>//测试Demo效果
$test = new Test();
$str = '要加密的内容sxfenglei';
$data = $test->strToHex($str);
echo '加密内容:要加密的内容sxfenglei <br>'.$data.'<hr>';
$output = $test->hexToStr($data);
echo '解密内容:E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569 <br>'.$output;
?>
版权属于:Joyber
本文链接:https://blog.qqvbc.com/default/78.html
转载时须注明出处及本声明