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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |