如何把PHP二进制与字符串之间的相互转换

2025年05月04日 20:18
有2个网友回答
网友(1):

header("Content-type: text/html; charset=utf-8");
/**
* 将字符串转换成二进制
* @param type $str
* @return type
*/
function StrToBin($str){
//1.列出每个字符
$arr = preg_split('/(?
//2.unpack字符
foreach($arr as &$v){
$temp = unpack('H*', $v); $v = base_convert($temp[1], 16, 2);
unset($temp);
}
return join(' ',$arr);
}
/**
* 讲二进制转换成字符串
* @par

网友(2):

使用pack与unpack函数,具体使用方式可看文档