in reply to Decmal to 16 bit binary number
There are two ways to pack the bytes in a 16-bit integer:
(Wikipedia The diagrams on the right part way down are clearest explanation to me.)
Update: Switched the descriptions around per proceng++ post below.
Here, the bits of the low-value byte are stored in the byte of the target location with the highest address
You can do this is perl using my $BEbin = pack 'n', 1263;
Here, the bits of the low-value byte are stored in the byte of the target location with the lowest address.
You can do this is perl using my $LEbin = pack 'v', 1263;
There are also two ways of unpacking the bits of either representation:
print unpack 'b16', $bin
print npack 'B16', $bin;
The result is 4 different ways of displaying the binary representation of a 16-bit number. By the example you gave, it would appear to be the last of those below that you are after:
print unpack 'B16', pack 'v', 1263;; 1110111100000100 print unpack 'B16', pack 'n', 1263;; 0000010011101111 print unpack 'b16', pack 'v', 1263;; 1111011100100000 print unpack 'b16', pack 'n', 1263;; 0010000011110111
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Decmal to 16 bit binary number
by proceng (Scribe) on Nov 01, 2007 at 13:32 UTC | |
by BrowserUk (Patriarch) on Nov 01, 2007 at 14:01 UTC |