tags:

views:

62

answers:

2

In PHP I have an array of bytes that I want to turn into a single variable.

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

I figured I could create a string like so:

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

but that seems hacky.

Any suggestions?

+1  A: 
$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.
dnagirl
Please format your code.
Gumbo
A: 

I ended doing the same thing as dna girl, but more verbose.

Also becaue the array is coming from an ini file, the array not being recognized as a hex values. so I added an 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. You shouldn't need to store your IVs if you pad correctly. (but that is another story...)

Lance Rushing