in reply to Practical Example of Converting Packed Value
I'd probably use just bitwise math, but as you've asked for pack, here's one way:
$s = chr( 0xa8 ) . chr( 0x38 );; print unpack 'b*', $s;; 0001010100011100 print unpack 'A5A4A7', unpack 'b*', $s;; 00010 1010 0011100 print map{ unpack 'C', pack 'b*', $_ } unpack 'A5A4A7', unpack 'b*', $ +s;; 8 5 28 ( $day, $month, $year ) = map{ unpack 'C', pack 'b*', $_ } unpack 'A5A4A7', unpack 'b*', $ printf "%4d-%02d-%02d\n", 1980+$year, $month, $day;; 2008-05-08
Updated: Or using bitwise math:
$s = chr( 0xa8 ) . chr( 0x38 );; $n = unpack 's', $s;; ( $day, $month, $year ) = ( $n & 0x1f, ( $n & 0x1e0 ) >> 5, ( $n & 0xfe00 ) >> 9 );; printf "%4d-%02d-%02d\n", 1980+$year, $month, $day;; 2008-05-08
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Practical Example of Converting Packed Value
by Jim (Curate) on Dec 01, 2008 at 00:30 UTC | |
by ikegami (Patriarch) on Dec 01, 2008 at 00:50 UTC | |
by Jim (Curate) on Dec 01, 2008 at 02:02 UTC | |
by BrowserUk (Patriarch) on Dec 01, 2008 at 02:40 UTC | |
by ikegami (Patriarch) on Dec 01, 2008 at 04:00 UTC |