Unexpected output with pack()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Unexpected output with pack()

Post by EricS »

I'm trying to pack a hex string into binary but I'm not getting the output I expect:

Code: Select all

var_dump(bin2hex(pack('H', 'ff')));
Returns

Code: Select all

string(2) "f0"
But shouldn't it be returning

Code: Select all

string(2) "ff"
Thank you in advance.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Unexpected output with pack()

Post by Chris Corbyn »

Actually that's correct.

H tells it to only read the first byte in the input string (F).

H* is what you need (FF).
Post Reply