4

I'm trying to write a binary file from hex string. For example, if my hex string is C27EF0EC, then the hex file should contain ASCII characters for C2, 7E, F0 and EC.
How do I do this in PHP?

Here's what I've tried:

$s="";
for ($i=0; $i<count($h); $i++) {
    $s+=pack("C*", "0x".$h[$i]);
}
$f2=fopen("codes0", "wb+");
fwrite($f2, $s);
4
  • What have you tried? Commented Dec 23, 2012 at 19:50
  • I believe you may find your answer [here][1] [1]: stackoverflow.com/questions/7488538/… Commented Dec 23, 2012 at 19:53
  • @MarkReed I edited it to my post. Commented Dec 23, 2012 at 19:53
  • @Taicho I didn't mean that. Commented Dec 23, 2012 at 20:02

2 Answers 2

2

So the first thing you need to do is turn your single string into an array of two-character strings with str_split.

$hex_bytes = str_split($h, 2);

Then you want to convert each of those values from a hexadecimal string to the corresponding number with hexdec.

$code_array = array_map(hexdec, $hex_bytes);

Then you want the byte value corresponding to each of those character codes, which you can get with chr:

$char_array = array_map(chr, $code_array);

Finally, you want to join all those bytes together into a single string, which you can do with implode.

$s = implode($char_array);

You can use the steps above in that order, or you can put it all together into one expression like this:

$s = implode(array_map(chr, array_map(hexdec, str_split($h,2))));

Note that as soon as you get a value above 0x7F it's no longer "ASCII".

Sign up to request clarification or add additional context in comments.

6 Comments

And it's still the same. :(
Then you did something else wrong, because that yields a completely different result from your code.
See edit. Your code was treating $h as an array of strings instead of a single string.
Got these errors: Notice: Use of undefined constant chr - assumed 'chr' in C:\xampp\htdocs\codebase\index.php on line 2106 Notice: Use of undefined constant hexdec - assumed 'hexdec' in C:\xampp\htdocs\codebase\index.php on line 2106
You must have an older version of PHP. Use strings for the function arguments: 'chr', 'hexdec', etc.
|
0

Assuming that array $binary is a previously constructed array bytes (like monochrome bitmap pixels in my case) that you want written to the disk in this exact order, the below code worked for me on an AMD 1055t running ubuntu server 10.04 LTS.

I iterated over every kind of answer I could find on the Net, checking the output (I used either shed or vi, like in this answer) to confirm the results.

<?php
$fp = fopen($base.".bin", "w");
$binout=Array();
for($idx=0; $idx < $stop; $idx=$idx+2 ){
    if( array_key_exists($idx,$binary) )
        fwrite($fp,pack( "n", $binary[$idx]<<8 | $binary[$idx+1]));
    else {
        echo "index $idx not found in array \$binary[], wtf?\n";
    }
}
fclose($fp);
echo "Filename $base.bin had ".filesize($base.".bin")." bytes written\n";
?>

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.