in reply to HowTo unpack a vec (bitstring)

Erm..
for my $i (0..20) { vec( $bitstring, $offset++, 4 ) = $i ; }
That sets consecutive nybbles (half-bytes) to 0,1,2,3 .. etc.

my $bits = unpack("A4*", $bitstring);
This "unpacks" an ascii string of 4 bytes (the * at the end doesn't do anything since you didn't give a format letter). In other words, it just returns the first 4 bytes of $bitstring without converting them in any way.

I don't think there's an unpack template for nybbles. If you want to see what you put in $bitstring, you might want to use the bit string template: "b*":

print unpack("b*",$bitstring);
output (broken up for readability):
0000100001001100001010 1001101110000110010101 1101001110110111111100 0010000100110000100000
or hex:
print unpack("h*",$bitstring);
output:
0123456789abcdef012340

Replies are listed 'Best First'.
Re^2: HowTo unpack a vec (bitstring)
by jeanluca (Deacon) on Jun 19, 2006 at 09:28 UTC
    Ok, I made the following changes:
    vec ($bitstring, $offset++,8) = $_ ;
    So now I can do
    my $bits = unpack("x1 C1", $bitstring);
    However I cannot do
    my $bits = unpack("x1 A1", $bitstring);
    So why is it not possible to extract an integer as an ASCII character ?

    LuCa
        I still don't understand, if it is possible, my above example should extract the numbers, right ?