say pack "A*", "asdf";
say pack "s", 0x41 * 256 + 0x42;
The first line makes sense: you take an ASCII encoded string, inserting it into the string as an ASCII string. The second line contains the packed form "\ x42 \ x41" due to the small finiteness of short integers on my machine.
However, I can’t shake the feeling that somehow I have to be able to treat the packed line from the second line as a number, since this, as I assume, Perl stores numbers, since the sequence of bytes is of lower order, Is there a way to do this without it unpacking? I am trying to get the right mental model for what pack () returns.
For example, in C, I can do this:
#include <stdio.h>
int main(void) {
char c[2];
short * x = c;
c[0] = 0x42;
c[1] = 0x41;
printf("%d\n", *x);
return 0;
}
source
share