here is my "go at it". Its been years since I looked at pack or unpack..But I would suggest when solving a problem like this, make a binary file first that is known to be big "endian" and then work from there. My code for the unpack looks a bit strange. Fletch has some great code above. Anyway make a binary file that you know to be right, then work from there. Packing is easier than unpacking different size data things. My binary "binout" file assumes 32 bit big endian and typical character byte packing for that architecture.
#!/usr/bin/perl -w use strict; my @x = (0x1230,0x1231,0x1232,0x1233,0x1234, 0x1235,0x1236,0x1237,0x1238,0x1239); my @c = qw (a b c d e f g h i j); open (BIN, '>', "binout") || die " unable to open binout"; binmode (BIN) || die "unable to set binmode"; print BIN pack('N'x10,@x); print BIN pack('a'x10,@c); close(BIN); open (BIN, '<', "binout") || die "CAN'T OPEN BINOUT ?!"; while(my $buff = <BIN>) { my (@tokens)= unpack ('N10C10', $buff); foreach my $digit (@tokens[0..9]) { printf "%x \n",$digit; } foreach my $char (@tokens[10..19]) { printf "%c\n", $char; } } __END__ binout is: (hex) 0000 1230 0000 1231 0000 1232 0000 1233 0000 1234 0000 1235 0000 1236 0000 1237 0000 1239 0000 1239 6162 6364 6566 6768 696a output: 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 a b c d e f g h i j

In reply to Re^3: unpack() to several variables by Marshall
in thread unpack() to several variables by Saladino

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.