$a = join ' ', unpack 'C*', $a;
That is roughly the same thing as:
The first part -- the unpack -- uses the template, 'C*', which reads like this: 'C' takes one byte and converts it to an unsigned char value (base 10). Note that per perldoc -f pack 'C' only works with byte-width characters. For Unicode you would probably use U, but that doesn't appear to be an issue in your case.@temparray = unpack 'C*', $a; $a = join ' ', @temparray;
The asterisk in the unpack template basically just means to repeat that 'C' template for as long as there are more bytes to unpack into unsigned char values. So the result is that you get a list of unsigned char values (which happen to be the ASCII values) corresponding to the characters (the bytes) in the original string.
The next line -- the join line -- just serves to concatenate together the list of unsigned char values into one long string with each value separated from the next by a single space character (presumably so you have some prayer of knowing where one unsigned char value ends and the next one starts in the string).
In tye's example, the @temparray is avoided by just allowing unpack to spill its list of unsigned char values into the parameter list of join.
Dave
In reply to Re: Re: Converting ascii to numbers (unpack)
by davido
in thread Converting ascii to numbers
by toonski
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |