Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
There's more than one way to do things
 
PerlMonks  

RE: Why, what, where to unpack?! (was: Files, unpack and text values)

by mdillon (Priest)
on Jun 26, 2000 at 02:34 UTC ( [id://19792]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Why, what, where to unpack?! (was: Files, unpack and text values)
in thread Files, unpack and text values

i believe that a single element consisting of 'H*' is the template you are interested in.

i'm not sure if it is correct to loop on a binary file handle like that, but it certainly doesn't make much sense to me: "for each line in this binary file" is a contradiction in terms, since binary files don't have any "lines".

i don't know why your hex editor breaks strings at that interval, but it seems a little narrow for you to store it that way. i would use 36 bytes per line with no spaces, myself.

the following code works fine for me. however, i would probably use MIME::Base64 to encode the data if it were my choice. plain hex is just a little too fat for my taste.

open IMG, "foo.gif" or die "Couldn't open image: $!\n"; undef $/; $image = <IMG>; print unpack("H*", $image); close IMG;
  • Comment on RE: Why, what, where to unpack?! (was: Files, unpack and text values)
  • Download Code

Replies are listed 'Best First'.
RE: RE: Why, what, where to unpack?! (was: Files, unpack and text values)
by BBQ (Curate) on Jun 26, 2000 at 03:13 UTC
    You are absolutely right! That did the trick... This is what I turned up with to get it looking somewhat with my hex editors output:
    open IMG, "foo.gif" or die "Couldn't open image: $!\n"; undef $/; $image = <IMG>; $hex = unpack("H*", $image); close IMG; while ($txt = substr($hex,0,32,'')) { $txt =~ s/(..)/$1 /g; print $txt."\n"; }
    Now all I have to do is figure out where those extra bytes went (the results are still not exactly the same), and who's goofing up on this one. I bet you its the editor...

    #!/home/bbq/bin/perl
    # Trust no1!
      the slurp/unpack method does not lose any bytes. i checked my code's round-trip integrity with cmp(1) like so:
      [mike@prometheus ~]$ perl -e 'undef $/; open IMG, "foo.gif"; print pack("H*", unpack("H*", <IMG>));' | cmp foo.gif -

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://19792]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.