converting to a binary number?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    converting to a binary number?

    I'm having some trouble finding a function that converts numbers
    (whether integer, hex, whatever) to its binary representation. Is there one?

    Thanks.
  • mensanator@aol.com

    #2
    Re: converting to a binary number?


    John Salerno wrote:[color=blue]
    > I'm having some trouble finding a function that converts numbers
    > (whether integer, hex, whatever) to its binary representation. Is there one?
    >
    > Thanks.[/color]

    Get the Gnu Multiple Precision library for Python module (Google GMPY).
    [color=blue][color=green][color=darkred]
    >>> import gmpy
    >>> help(gmpy.digit s)[/color][/color][/color]
    Help on built-in function digits:
    digits(...)
    digits(x[,base]): returns Python string representing x in the
    given base (2 to 36, default 10 if omitted or 0); leading '-'
    present if x<0, but no leading '+' if x>=0. x must be an mpz,
    or else gets coerced into one.
    [color=blue][color=green][color=darkred]
    >>> for i in range(16):[/color][/color][/color]
    print gmpy.digits(i,2 )

    0
    1
    10
    11
    100
    101
    110
    111
    1000
    1001
    1010
    1011
    1100
    1101
    1110
    1111

    Comment

    • astyonax

      #3
      Re: converting to a binary number?

      that's great! I will try! :-))

      Comment

      • Kent Johnson

        #4
        Re: converting to a binary number?

        John Salerno wrote:[color=blue]
        > I'm having some trouble finding a function that converts numbers
        > (whether integer, hex, whatever) to its binary representation. Is there one?[/color]

        You might like this recipe:


        Kent

        Comment

        • John Salerno

          #5
          Re: converting to a binary number?

          mensanator@aol. com wrote:[color=blue]
          > John Salerno wrote:[color=green]
          >> I'm having some trouble finding a function that converts numbers
          >> (whether integer, hex, whatever) to its binary representation. Is there one?
          >>
          >> Thanks.[/color]
          >
          > Get the Gnu Multiple Precision library for Python module (Google GMPY).[/color]

          Thanks!

          Comment

          Working...