0

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?

2
  • What do you mean, "single variable"? Commented Sep 18, 2009 at 18:19
  • I want one variable to contain all the bytes, so I can pass it to another function (mcrypt stuff). Commented Sep 18, 2009 at 19:01

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.