2009-09-18 97 views
0

在PHP中我有一个字节数组,我想把它变成一个变量。从字节数组中创建一个变量

$bytes = array(0x12, 0x8D, 0x9D, 0x40, 0x09, 0x64, 0x5A, 0x6E); 

我想我可以创建一个字符串,像这样:

$string = chr(0x12).chr(0x8D)......; 

但似乎哈克。

有什么建议吗?

+0

你是什么意思,“单变量”? – 2009-09-18 18:19:24

+0

我想要一个变量包含所有的字节,所以我可以将它传递给另一个函数(mcrypt的东西)。 – 2009-09-18 19:01:55

回答

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. 
+0

请格式化您的代码。 – Gumbo 2009-09-18 18:37:26

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 

附:如果您正确填充,则不需要存储IV。 (但那是另一回事......)