1

I have to write ascii digits (not letters) like hex chars. One byte have to contain 2 digits (one per semibyte), suppose this is the string i have '0665879955443322' to write in file and this result 06 65 87 99 55 44 33 22 (8 bytes) i want have! (like a packed format) I try this examples that do something similar

php

$a=0x06;
$b=0x65;
$c=0x87;
$txt = pack('CCC',$a,$b,$c) . "\x0A"; # Hex output is: 06 58 0A
$txt = "\x06" . "\x58" . "\x0C" . "\x0A";

# write to file to view with exadecimal editor
$bytes = file_put_contents('test',$txt);
echo "$bytes written<br>";

-----------

Now my code that doesn't wrok !

$a="06";
$b="65";
$c="87";

//$txt = "\x06" . "\x58" . "\x0C" . "\x0A"; // this works
$txt = "\x" . $a" . "\x" . $b . "\" . $c . "\x0A";  // this not work !
$bytes = file_put_contents('test',$txt);
echo "$bytes written<br>";

---------

HOW TO TRASFER $a $b $c LIKE TO BE BYTES ?

Thanks lot

  • `$txt = chr(hexdec($a)).chr(hexdec($b)).chr(hexdec($c))."\x0A";`, If the bytes are in hex (which seems likely). If not. just drop de `hexdec`. – Wrikken Mar 15 '14 at 11:00

0 Answers0