php 16进制与字符串的转换

<?php
# 字符串转16进制
$string = "LockDataV";
$arr1   = str_split($string, 1);
foreach ($arr1 as $akey => $aval) {
	$arr1[$akey] = "\x" . bin2hex($aval);
}
echo "<pre>";
var_dump($arr1);

# 16进制转字符
function hexToStr($hex) {
    $str = "";
    for ($i = 0;$i < strlen($hex) - 1;$i+= 2) $str.= chr(hexdec($hex[$i] . $hex[$i + 1]));
    return $str;
}

$arrs1 = ["\x4c", "\x6f", "\x63", "\x6b", "\x44", "\x61", "\x74", "\x61", "\x56"];
$arr2 = $arr1;
foreach ($arr2 as $skey => $sval) {
	$arr2[$skey] = hexToStr($sval);
}
echo "<pre>";
var_dump($arr2);

echo "<pre>";
$arr3 = implode("", $arr2);
var_dump($arr3);


?>

  

posted @ 2022-04-19 18:05  tochenwei  阅读(619)  评论(0编辑  收藏  举报