@@ -41,11 +41,10 @@ of these two functions.
41
41
42
42
To see how (un)packing works, we'll start with a simple template
43
43
code where the conversion is in low gear: between the contents of a byte
44
- sequence and a string of hexadecimal digits. Let's use C<unpack>, since
45
- this is likely to remind you of a dump program, or some desperate last
46
- message unfortunate programs are wont to throw at you before they expire
47
- into the wild blue yonder. Assuming that the variable C<$mem> holds a
48
- sequence of bytes that we'd like to inspect without assuming anything
44
+ sequence and a string of hexadecimal digits. The example we've chosen
45
+ looks like a crash dump, and shows how C<unpack> can be used to make
46
+ sense of things like this. Assuming that the variable C<$mem> holds a
47
+ sequence of bytes that we'd like to inspect without assuming anything
49
48
about its meaning, we can write
50
49
51
50
my( $hex ) = unpack( 'H*', $mem );
@@ -58,17 +57,21 @@ corresponding to a byte:
58
57
59
58
What was in this chunk of memory? Numbers, characters, or a mixture of
60
59
both? Assuming that we're on a computer where ASCII (or some similar)
61
- encoding is used: hexadecimal values in the range C<0x40 > - C<0x5A>
60
+ encoding is used: hexadecimal values in the range C<0x41 > - C<0x5A>
62
61
indicate an uppercase letter, and C<0x20> encodes a space. So we might
63
- assume it is a piece of text, which some are able to read like a tabloid;
64
- but others will have to get hold of an ASCII table and relive that
65
- firstgrader feeling. Not caring too much about which way to read this,
62
+ assume it is a piece of ASCII text. We can verify that by doing
63
+
64
+ print unpack( 'A*', $mem), "\n"
65
+
66
+ A MAN A PLAN A CANAL PANAMA
67
+
68
+ Going back to the original C<H*> for the purposes of this tutorial,
66
69
we note that C<unpack> with the template code C<H> converts the contents
67
70
of a sequence of bytes into the customary hexadecimal notation. Since
68
71
"a sequence of" is a pretty vague indication of quantity, C<H> has been
69
72
defined to convert just a single hexadecimal digit unless it is followed
70
73
by a repeat count. An asterisk for the repeat count means to use whatever
71
- remains.
74
+ remains. That is also the case for the C<A*> above.
72
75
73
76
The inverse operation - packing byte contents from a string of hexadecimal
74
77
digits - is just as easily written. For instance:
0 commit comments