in reply to 3-byte representation
If you want big-endian byte order:
pack: $s = substr(pack('l>', $n), 1); unpack: $n = unpack('l>', "$s\0")/256;
If you want little-endian byte order:
pack: $s = substr(pack('l<', $n), 0, 3); unpack: $n = unpack('l<', "\0$s")/256;
PS — Going from 3 to 4 increases the space needed by 33%, not 25%.
Update: Slightly simpler solutions.
Update: The /256 got dropped when I posted my solution. Fixed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: 3-byte representation
by BrowserUk (Patriarch) on Oct 12, 2011 at 18:32 UTC | |
by ikegami (Patriarch) on Oct 12, 2011 at 18:45 UTC | |
|
Re^2: 3-byte representation
by BrowserUk (Patriarch) on Oct 12, 2011 at 19:10 UTC | |
|
Re^2: 3-byte representation
by BrowserUk (Patriarch) on Oct 12, 2011 at 19:12 UTC | |
by Anonymous Monk on Oct 13, 2011 at 10:48 UTC | |
by BrowserUk (Patriarch) on Oct 13, 2011 at 12:36 UTC | |
by gerleu (Novice) on Oct 13, 2011 at 14:21 UTC | |
by BrowserUk (Patriarch) on Oct 13, 2011 at 15:40 UTC | |
| |
by ikegami (Patriarch) on Oct 13, 2011 at 16:38 UTC | |
by BrowserUk (Patriarch) on Oct 13, 2011 at 16:40 UTC | |
by ikegami (Patriarch) on Oct 13, 2011 at 17:09 UTC |