home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: Use of 'b' templates
- Message-ID: <1992Sep11.183509.23437@netlabs.com>
- Date: 11 Sep 92 18:35:09 GMT
- References: <memo.619212@cix.compulink.co.uk>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 29
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <memo.619212@cix.compulink.co.uk> pmoore@cix.compulink.co.uk writes:
- : The subject says it, really. Can somebody tell me how 'b' and 'B' templates
- : work, in pack() and unpack()? Preferably with a few examples to make things
- : clear.
- :
- : I have just struggled to an understanding of vec(), but 'b' is beyond me...
- :
- : [NB: if it matters for examples, my machine stores bytes such that
- :
- : for $i (0..15)
- : {
- : print vec('31',$i,1) ? '1' : '0';
- : }
- :
- : gives 1100110010001100]
-
- Saying unpack('b*', '31') gives you the same as vec(). Saying
- unpack('B*', '31) gives you the same thing, only with the bits reversed
- in each byte. In other words, b starts with the 1 bit, and B starts
- with the 128 bit. Since binary numbers are usually written big-endian,
- use B to display binary numbers. Since vec() starts storing bits at
- the low end, use b to display vec() values.
-
- Use pack to translate the other direction.
-
- The h and H templates specifiers work just the same, only nybble by nybble
- rather than bit by bit.
-
- Larry
-