in reply to Recreating a binary file from hex representation

You should use pack and unpack as was pointed out already, but the error in your script is here:
printf("%X", ord($buffer))
One Byte can be 0 to FF in hex. If you have a stream of hex characters, how can you know you have 1A (26 in decimal) or two numbers, 1 and 10?

say

printf("%02X", ord($buffer))
to make shure each byte is represented by 2 chars (e.g. 0A for decimal 10 instead of just A). Converting back you have to read 2 Bytes each time.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}