0
$a = pack("v","0xABCD");
$b = pack("v",0xABCD);
$w = "test.bin";
$fp = fopen($w,"wb");
$v = fwrite($fp,$a.$b);
$v = fclose($fp);

When I look at the hexdump, it shows : 00 00 CD AB. The string content of $a is ignored, $b is correctly inserted. How can I fix that, as the hex values are generated as strings ?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Petoetje59
  • 113
  • 1
  • 5
  • https://stackoverflow.com/questions/14674834/php-convert-string-to-hex-and-hex-to-string – BetaDev Jun 16 '17 at 22:00
  • I was afraid someone would refer me to that. That's NOT the answer to my question, this treats the contents as separate CHARACTERS, not as HEX VALUES. I really looked at all the previous posts here, and nothing pertains to my problem. – Petoetje59 Jun 17 '17 at 08:23
  • 1
    @Petoetje59 if you really looked at all the previous posts, then explain in your question which ones you looked at and how those did not address your issue. From what little information you gave, the very first question I would pose is "this is a question about how `pack` works, did you read its documentation and all the comments tacked onto it by the community", because pack takes a formattng tempate and numerical data, and you're passing it a formatting template and string data when assigning `$a` – Mike 'Pomax' Kamermans Jun 17 '17 at 12:05

1 Answers1

0

Got it to work myself using some tweaking, a little-used PHP string function, and alternate pack() format codes :

$a=pack("h*",strrev("ABCD"));
$b=pack("v",0xABCD); // Correct
$w="test.bin";$fp=fopen($w,"wb");$v=fwrite($fp,$a.$b);$v=fclose($fp);

Both are shown the same now.

FWIW.

Petoetje59
  • 113
  • 1
  • 5