My brain hurts with all this pack, unpack gibberish.
You have a simple situation.
I will attempt to explain: my @bytes = unpack ('C*',$buf); is the key thing you need to understand.
#!/usr/bin/perl; use strict; use warnings; # ! / u s r / b + i n / my $buf = pack ('C*', (0x23, 0x21, 0x2F, 0x75, 0x73, 0x72, 0x2f, 0x62, + 0x69, 0x6E, 0x2f)); # $buf is now a sequence of "characters" which are 8 bit unsigned num +bers # Don't go all crazy with me about multi-byte characters # Here "C" means 8 bits, one byte # The ASCII characters corresponding to those numbers are: # #!/usr/bin/ # to get a particular characters or a group of characters # from the $buf string, use substr() # # substr EXPR,OFFSET,LENGTH,REPLACEMENT # substr() is often used in conjection with unpack to generate # a particular numeric value with different byte ordering of say # a 16 or 32 or 64 bit value print substr($buf,3,3); #prints: usr print "\n"; print substr($buf,7,4); #prints: bin/ print "\n"; # Now translate each byte in $buf into an array.. # Each 8 bit character will be a represented on my # computer as a 64 bit signed value in an array of # what are named "bytes" my @bytes = unpack ('C*',$buf); # @bytes is now an array of numbers! # in decimal values: print "Decimal Values of \@bytes\n"; print "$_ " for @bytes; print "\n"; # Now to print those bytes in character context: print "Character values of \@bytes\n"; print chr($_) for @bytes; print "\n"; __END__ usr bin/ Decimal Values of @bytes 35 33 47 117 115 114 47 98 105 110 47 Character values of @bytes #!/usr/bin/

Update:
I looked back over some code from 2001:
This modifies the header of a wave file.
- creates byte strings in the correct order ("little endian")
- then replaces, in 2 places, the 32 bit values in the buffer with new values
- then writes the buffer with the new size parameters

Hope this helps give you some ideas...
How to replace a 32 bit value in a binary buffer with a new value...
I might do it differently now, but after 18 years, this code still makes "sense"

my $rsize = pack("V4", $new_riff_size); # "V4" means Vax or Intel "lit +tle endian" substr($buff,4,4) = substr($rsize,0,4); my $data_size = pack("V4", $new_data_size); substr($buff,54,4)= substr($data_size,0,4); print OUTBIN substr($buff,0,58);

In reply to Re^2: Accessing individual bytes of a binary file as numerical values by Marshall
in thread Accessing individual bytes of a binary file as numerical values by Chris01234

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.