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

Decmal to 16 bit binary number

by isha (Sexton)
on Nov 01, 2007 at 06:23 UTC ( [id://648438]=perlquestion: print w/replies, xml ) Need Help??

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

How to convert decimal number to 16 bits wide binary number using sprintf?
Eg: 1263 is converted to 0010000011110111

Replies are listed 'Best First'.
Re: Decmal to 16 bit binary number
by BrowserUk (Patriarch) on Nov 01, 2007 at 07:33 UTC

    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.

    1. Big-endian.

      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;

    2. Little-endian

      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:

    1. Least significant bit (lsb) first, progressing to most significant bit (msb).

      print unpack 'b16', $bin

    2. msb first progressing to lsb.

      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

    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.
      1. Big-endian. 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 $BEbin = pack 'n', 1263;
      2. Little-endian 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 $LEbin = pack 'v', 1263;
      Shouldn't this be the other way around?
      In Big-endian, the bytes are stored in memory in order eg: 0x1263 would be 0x1263.
      In Little-endian, 0x1263 would be stored as 0x6312.

      Update: Edited for formatting

Re: Decmal to 16 bit binary number
by ikegami (Patriarch) on Nov 01, 2007 at 06:35 UTC
    my $uint16 = pack 'n', 1263; # "\x12\x63"

    Or if you're asking for the binary representation of the number,

    my $bin = unpack 'B16', pack 'n', 1263; # "0000010011101111"
Re: Decmal to 16 bit binary number
by dsheroh (Monsignor) on Nov 01, 2007 at 06:35 UTC
    (s)printf won't do exaxtly what you asked for by itself, since you appear to have reversed the order of the bits in each byte. Assuming that's just an error in entering the question,
    $ perl -e 'printf "%016b\n", 1263' 0000010011101111
    converts the integer to a 16-bit binary representation. If you actually do want to reverse the bits in each byte, split or substr and reverse should provide ways to rearrange their order.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found