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);
|