在PHP中,我有一个字节数组,我希望将其转换为单个变量。
$bytes = array(0x12, 0x8D, 0x9D, 0x40, 0x09, 0x64, 0x5A, 0x6E);
我想我可以像这样创建一个字符串:
$string = chr(0x12).chr(0x8D)......;
但这看起来很糟糕。
有什么建议吗?
答案 0 :(得分:1)
$string=implode('',array_map('chr',array(0x8D, 0x9D, 0x40, 0x09, 0x64, 0x5A, 0x6E)));
echo $$string; //given that you also have a variable named whatever the bytecode translates to.
答案 1 :(得分:0)
我结束了与dna女孩做同样的事情,但更加冗长。
此外,由于数组来自ini文件,因此数组不会被识别为十六进制值。所以我添加了一个intval()。
function getInitializationVector() {
$ini = parse_ini_file('foo.ini');
$stringVector = explode(',', $ini['initialization_vector'] );
$iv = '';
foreach($stringVector as $theByte) {
$iv .= chr(intval($theByte, 16));
}
return $iv;
}
foo.ini:
initialization_vector=0x8D,0x9D,0x40,0x09,0x64,0x5A,0x6E,0xD4
P.S。如果正确填充,则不需要存储IV。 (但那是另一个故事......)