# reading (for example, a gif) local $/ = undef; open FILE, $file; binmode(FILE); # needed for win32 $gif = <FILE>; $hexgif = unpack ("H*", $gif); # convert gif to hex string # printing $gif = pack ("H*", $hexgif); # convert hex string back to binary data print "Content-type: image/gif\n\n"; binmode(STDOUT); print $gif;
Update: jeroenes is right about the size consideration. You asked for hex so that's what this code does. But a hex string is double the size of the binary data is encodes. Here are the results of some different methods for encoding binary data as text:
bytes format % of original 5008 original jpeg 100.00% 10016 hex string 200.00% 6904 uuencoded 137.86% 6768 base64 135.14% 6706 zlib/uuencoded 133.91% 6574 zlib/base64 131.27% 4865 zlib (binary) 97.14%
In reply to Re: Re: Hex format conversion in Perl
by epoptai
in thread Hex format conversion in Perl
by mkadlec
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |