jeroenes, did you forget the hex conversion?
(his reply is now below)
# 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:

bytesformat% 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.