in reply to using pack() to build a 16 bit word
If you use format strings like 'b6 b5 b3 b2', each unit will be padded out to full byte width, rather than being all jammed together.my ($pos, $aw, $stru) = (2, 5, 6); my $bits = sprintf('%06b%05b%03b%02b', 0, $stru, $aw, $pos); print "Bits=<$bits>\n"; my $bstr=pack('b16', $bits); printf "<$bstr> is %d bytes\n", length($bstr); print "\nUnpack: ", unpack('B*', $bstr), "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: using pack() to build a 16 bit word
by fizbin (Chaplain) on May 12, 2004 at 12:33 UTC |