I've tried packing and unpacking every way I know how ...

Last night, I was hacking, packing and unpacking because my understanding of those operations was lacking.

I hope that you will find my examples below of some help.
Bonus question: is my machine big-endian or little-endian?

Rudif

#! perl -w use strict; my $string = uc " hack && pack || unpack"; #my $string = 'abcdefgh'; #my $string = uc 'abcdefgh'; #my $string = 'ABCD'; my @hex = unpack 'H*', $string; print "H @hex\n"; my @bytes = unpack 'C*', $string; print "C @bytes\n"; my @revbytes = reverse @bytes; my $revbytes = pack 'C*', @revbytes; print "C $revbytes\n"; my @shorts = unpack 'S*', $string; print "S @shorts\n"; my @revshorts = reverse @shorts; my $revshorts = pack 'S*', @revshorts; print "S $revshorts\n"; my @longs = unpack 'L*', $string; print "L @longs\n"; my @revlongs = reverse @longs; my $revlongs = pack 'L*', @revlongs; print "L $revlongs\n"; my @bits = unpack 'B*', $string; print "B @bits\n"; my @revbits = reverse @bits; my $revbits = pack 'B*', @revbits; print "B $revbits\n"; ++$_ foreach @bytes; my $bytes = pack 'C*', @bytes; print "C $bytes\n"; __END__ H 204841434b202626205041434b207c7c20554e5041434b C 32 72 65 67 75 32 38 38 32 80 65 67 75 32 124 124 32 85 78 80 65 6 +7 75 C KCAPNU || KCAP && KCAH S 18464 17217 8267 9766 20512 17217 8267 31868 21792 20558 17217 S ACNP U||K AC P&&K AC H L 1128351776 640032843 1128353824 2088509515 1347310880 L UNPK || PACK && HAC B 001000000100100001000001010000110100101100100000001001100010011000 +100000010100000100000101000011010010110010000001111100011111000010000 +0010101010100111001010000010000010100001101001011 B HACK && PACK || UNPACK C !IBDL!''!QBDL!}}!VOQBDL

In reply to Re: Constructing 32-bit hexadecimal integers by Rudif
in thread Constructing 32-bit hexadecimal integers by donfreenut

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.