I've never groked converting bit vectors, bitmaps, packed fields or any Perl bitwise operations and it's high time I do. I want to learn something using a practical example of my own that I kind of understand already rather than trying to puzzle through unfamiliar examples in a book, manpage or tutorial.

Let's say I have these two bytes in a variable: 0xA8 0x38. ("Are these two bytes a string or a 16-bit integer?" you ask. I don't know; you tell me. This is part and parcel of my profound ignorance of this stuff. Pretend I just read the two bytes from a file into a variable.) These 16 bits are a packed representation of an MS-DOS FAT date.

I want to use Perl bitwise operations to convert this 16-bit integer (or string) to the date that it represents: 2008-05-08. Here's the conversion as I (naïvely) understand it:

A8 38 Hexadecimal (Little Endian) 10101000 00111000 Binary (Little Endian) 38 A8 Hexadecimal (Big Endian) 00111000 10101000 Binary (Big Endian) 11111100 00000000 54321098 76543210 Bit Ruler (Indexed From 0) 0011100 0101 01000 Binary (Regrouped) 1111110 0000 00000 5432109 8765 43210 Bit Ruler (Regrouped) Year Month Day Packed Format 0011100 0101 01000 Binary 1C 05 08 Hexadecimal 28 5 8 Decimal Year = 1980 + 28 = 2008 Month = 5 Day = 8 MS-DOS FAT Date = 2008-05-08
How do I accomplish this conversion in Perl? All hints are welcome. Remember: I'm seeking genuine learning more than a solution to a problem -- and especially not the answer to a homework problem, I promise. IANAS!

Jim


In reply to Practical Example of Converting Packed Value by Jim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.