Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Decimal -> Binair

    Does anyone know a good way to convert a decimal number to the binair-system?

    (I'd prefer a sub, returning the binair value)

    Thanks in advance.

    Mzzl, Chris

  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stolen from Perl Book...

    Lol...

    $myinput=70323;
    vec($number,0,32)=$myinput;
    $bitstring=unpack("B*", $number);
    print $bitstring;


    Set the 32 number to the number of bits you actually need if you don't want preceeding zeros - but it must be a binary power of two to be valid, ie, 1,2,4,8,16,32 (maximum).

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks man, the code works.

    But I'd like to understand it too...
    I'm not quiet a newB to Perl, but I don't understand the code, could you explain it for me?

    Thanks again.

    Mzzl, Chris

  • #4
    New Coder
    Join Date
    Aug 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Oh Sure!

    I'll have it figured out as soon as I break out the Perl source code for the VEC function!

    Honestly I don't have a single clue as to how the VEC function works... Out of three really good books, the longest mention of it is less than half a page.

    I just lucked out in finding it.... Credit the books for this one!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •