home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5873 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  1.4 KB

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