pack is the wrong function for what you are trying to do, for two reasons. First, pack() really doesn't deal with things smaller than bytes (which is not completely obvious from the documentation). Second, pack() gives you a string whose bytes represent binary values in various formats and what you want is a numeric value expressed as hexademical.

Putting the bits together into a numeric value is much closer to your working extraction code but using | instead of & and << instead of >>:

my $date = $day | ($month<<5) | ($year<<9); my $time = $second | ($min<<5) | ($hour<<11); my $fat32 = sprintf "0x%08x", $time | ($date<<16);

Update: Another approach is to use a base-2 string of '0' and '1' characters, using Perl's support for numbers like 0b10011 (also via oct) and sprintf's %b format, like so:

my $bits = sprintf "%07b%04b%05b%05b%06b%05b", $year, $month, $day, $hour, $min, $sec; my $fat32 = sprintf "0x%08x", oct( "0b$bits" );

Here is example data from testing those two snippets:

Sat Jul 25 16:09:52 2015 Binary: year mon day hour min sec 0100011 0111 11001 10000 001001 11010 35 7 25 16 9 26 2015 52 Hex: 0100 0110 1111 1001 1000 0001 0011 1010 0x46f9813a

- tye        


In reply to Re: Pack + Fat32 timestamp (|, <<) by tye
in thread Pack + Fat32 timestamp by tbr123psu

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.