2009-09-18 79 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。 (但那是另一回事......)