Your Mother has asked for the wisdom of the Perl Monks concerning the following question:

I am embarrassed to ask. In my defense I figured out quite a bit of my problem already (there were six or seven parts and I'm stuck on this, the last, one); I'm just not that good at this so–

What unpack template do I need to turn the 8 bytes -- 00 00 01 16 7A 53 7C 80 (meaning I don't have a hex string but a byte stream) -- into a timestamp November 26,2007 (so, this might be 1196060400 but I'm not sure it won't be somewhere else in the day.

I tried everything I could think to try. All the flags plain templates (even ones that didn't make any sense). Splitting the bytes up into groups and reversing them before unpacking with "L*" or friends. What's the right way?

Thank you!

  • Comment on Little help for the (un)pack challenged

Replies are listed 'Best First'.
Re: Little help for the (un)pack challenged
by BrowserUk (Patriarch) on Oct 01, 2009 at 07:15 UTC

    Your description of the input format is lacking.

    If those numbers represent the numeric values of the bytes of a string, then this is just reconstructing that string from the information supplied:

    my $bytes = pack 'C*', 0x00, 0x00, 0x01, 0x16, 0x7A, 0x53, 0x7C, 0x80;

    And that can be decoded as a 64-bit network-order unsigned integer using:

    print unpack 'Q', scalar reverse $bytes;; 1196053200000

    If you have those numeric values in an array of scalars somewhere, you can skip a level:

    print unpack 'Q', pack 'C8', reverse 0x00, 0x00, 0x01, 0x16, 0x7A, 0x +53, 0x7C, 0x80;; 1196053200000

    Update: And if you have 5.10 with the fancy new endian modifiers, then:

    print unpack 'Q>', pack 'C8', 0x00, 0x00, 0x01, 0x16, 0x7A, 0x53, 0x7 +C, 0x80;; 1196053200000

    If you don't have a 64-bit integer capable perl, it takes a little more work:

    ( $hi, $lo ) = unpack 'NN', $bytes;; print +($hi * 2**32 ) + $lo;; 1196053200000

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      You and almut rule (it's indeed millisecond based though the spec I have says it is plain epoch seconds). The last NN-based version works for me. I don't have 64 bits on this thing. It would have taken me a couple of days to figure that out myself. Thank you!

Re: Little help for the (un)pack challenged
by almut (Canon) on Oct 01, 2009 at 07:38 UTC

    Apparently, the number/timestamp is in milliseconds:

    my $t_msec = unpack "Q", reverse "\x00\x00\x01\x16\x7a\x53\x7c\x80"; print scalar gmtime($t_msec/1000); # Mon Nov 26 05:00:00 2007