Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Pack + Fat32 timestamp (|, <<)

by tye (Sage)
on Jul 25, 2015 at 22:57 UTC ( [id://1136315]=note: print w/replies, xml ) Need Help??


in reply to Pack + Fat32 timestamp

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        

Replies are listed 'Best First'.
Re^2: Pack + Fat32 timestamp (|, <<)
by tbr123psu (Novice) on Jul 25, 2015 at 23:32 UTC

    tye:

    thank you a million times! you absolutely, hands down, no doubt win my internet hero of the week award! the very first post, pre-edit, did the trick!

    i was thinking that felt right, but the & was throwing me off and i wasn't making any progress, so i went off in search of other means and that's how i stumbled across pack. Thanks for the explanation of why pack was the wrong choice! your post was both helpful and educational! very much appreciated.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1136315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found