Base conversion method or module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Wagner

    Base conversion method or module

    Is there a Python module or method that can convert between numeric bases? Specifically, I need to
    convert between Hex, Decimal and Binary such as 5Ah = 90d = 01011010b.

    I searched many places but couldn't find a Python specific one.

    Thanks, Jeff
  • Francis Avila

    #2
    Re: Base conversion method or module


    Jeff Wagner wrote in message <0bu6tvsde8jqvj lu09itnuq6et04t mk9dc@4ax.com>. ..[color=blue]
    >Is there a Python module or method that can convert between numeric bases?[/color]
    Specifically, I need to[color=blue]
    >convert between Hex, Decimal and Binary such as 5Ah = 90d = 01011010b.
    >
    >I searched many places but couldn't find a Python specific one.
    >
    >Thanks, Jeff[/color]

    There was a Python cookbook recipe that did these kinds of conversions,
    IIRC. Look around at http://aspn.activestate.com/ASPN/Cookbook/Python.
    Numeric might do this sort of thing, too, but I don't know.

    Python itself can get you pretty far; the problem is that it's a bit spotty
    in making conversions communicable.

    For example, int() and long() both accept a string with a base argument, so
    you can convert just about any base (2 <= base <= 36) to a Python int or
    long.

    Python can also go the other way around, taking a number and converting to a
    string representation of the bases you want. The problem? There's only a
    function to do this for hex and oct, not for bin or any of the other bases
    int can handle.

    Ideally, there's be a do-it-all function that is the inverse of int(): take
    a number and spit out a string representation in any base (2-36) you want.

    But the binary case is pretty simple:

    def bin(number):
    """bin(numb er) -> string

    Return the binary representation of an integer or long integer.

    """
    if number == 0: return '0b0'
    binrep = []
    while number >0:
    binrep.append(n umber&1)
    number >>= 1
    binrep.reverse( )
    return '0b'+''.join(ma p(str,binrep))


    Dealing with negative ints is an exercise for the reader....

    Remember also that Python has hex and octal literals.
    --
    Francis Avila

    Comment

    • Jeff Wagner

      #3
      Re: Base conversion method or module

      On Sun, 7 Dec 2003 15:45:41 -0500, "Francis Avila" <francisgavila@ yahoo.com> wrotf:
      [color=blue]
      >
      >Jeff Wagner wrote in message <0bu6tvsde8jqvj lu09itnuq6et04t mk9dc@4ax.com>. ..[color=green]
      >>Is there a Python module or method that can convert between numeric bases?[/color]
      >Specifically , I need to[color=green]
      >>convert between Hex, Decimal and Binary such as 5Ah = 90d = 01011010b.
      >>
      >>I searched many places but couldn't find a Python specific one.
      >>
      >>Thanks, Jeff[/color]
      >
      >There was a Python cookbook recipe that did these kinds of conversions,
      >IIRC. Look around at http://aspn.activestate.com/ASPN/Cookbook/Python.
      >Numeric might do this sort of thing, too, but I don't know.
      >
      >Python itself can get you pretty far; the problem is that it's a bit spotty
      >in making conversions communicable.
      >
      >For example, int() and long() both accept a string with a base argument, so
      >you can convert just about any base (2 <= base <= 36) to a Python int or
      >long.
      >
      >Python can also go the other way around, taking a number and converting to a
      >string representation of the bases you want. The problem? There's only a
      >function to do this for hex and oct, not for bin or any of the other bases
      >int can handle.
      >
      >Ideally, there's be a do-it-all function that is the inverse of int(): take
      >a number and spit out a string representation in any base (2-36) you want.
      >
      >But the binary case is pretty simple:
      >
      >def bin(number):
      > """bin(numb er) -> string
      >
      > Return the binary representation of an integer or long integer.
      >
      > """
      > if number == 0: return '0b0'
      > binrep = []
      > while number >0:
      > binrep.append(n umber&1)
      > number >>= 1
      > binrep.reverse( )
      > return '0b'+''.join(ma p(str,binrep))
      >
      >
      >Dealing with negative ints is an exercise for the reader....
      >
      >Remember also that Python has hex and octal literals.[/color]

      Francis,

      I found the Python cookbook recipe you were referring to. It is as follows:

      The module name is BaseConvert.py .......

      #!/usr/bin/env python

      BASE2 = "01"
      BASE10 = "0123456789 "
      BASE16 = "0123456789ABCD EF"
      BASE62 = "ABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789abcdefgh ijklmnopqrstuvw xyz"

      def convert(number, fromdigits,todi gits):

      if str(number)[0]=='-':
      number = str(number)[1:]
      neg=1
      else:
      neg=0

      # make an integer out of the number
      x=long(0)
      for digit in str(number):
      x = x*len(fromdigit s) + fromdigits.inde x(digit)

      # create the result in base 'len(todigits)'
      res=""
      while x>0:
      digit = x % len(todigits)
      res = todigits[digit] + res
      x /= len(todigits)
      if neg:
      res = "-"+res

      return res

      I am getting an error when I import this module and call it.

      #!/usr/bin/python

      import BaseConvert
      print BaseConvert.con vert(90,BASE10, BASE2)

      Name Error: name 'Base10' is not defined.

      This probably has something to do with namespaces which was biting me a while ago. I thought that
      since the 'Base' definitions were global to this module (BaseConvert.py ) by being defined outside
      the function (convert), that when I imported this module, they would be global, too.

      From one of my books, it says, "An import statement creates a new namespace that contains all the
      attributes of the module. To access an attribute in this namespace, use the name of the module
      object as a prefix: import MyModule ... a = MyModule.f( )" which is what I thought I was
      doing.

      What am I still missing?

      Thanks,
      Jeff

      Comment

      • Fredrik Lundh

        #4
        Re: Base conversion method or module

        Jeff Wagner wrote:
        [color=blue]
        > I found the Python cookbook recipe you were referring to. It is as follows:[/color]

        (what's wrong with just posting an URL?)
        [color=blue]
        > I am getting an error when I import this module and call it.
        >
        > #!/usr/bin/python
        >
        > import BaseConvert
        > print BaseConvert.con vert(90,BASE10, BASE2)
        >
        > Name Error: name 'Base10' is not defined.
        >
        > This probably has something to do with namespaces which was biting me
        > a while ago. I thought that since the 'Base' definitions were global to this
        > module (BaseConvert.py ) by being defined outside the function (convert),
        > that when I imported this module, they would be global, too.[/color]

        in Python, "global" means "belonging to a module", not "visible in all
        modules in my entire program"
        [color=blue]
        > What am I still missing?[/color]

        change the call to use BaseConvert.BAS E10 and BaseConvert.BAS E2

        to learn more about local and global names, read this:

        The official home of the Python Programming Language


        </F>




        Comment

        • Jeff Wagner

          #5
          Re: Base conversion method or module

          On Mon, 8 Dec 2003 00:40:46 +0100, "Fredrik Lundh" <fredrik@python ware.com> wrotf:
          [color=blue]
          >Jeff Wagner wrote:
          >[color=green]
          >> I found the Python cookbook recipe you were referring to. It is as follows:[/color]
          >
          >(what's wrong with just posting an URL?)[/color]

          What a great idea ;) ...

          [color=blue][color=green]
          >> I am getting an error when I import this module and call it.
          >>
          >> #!/usr/bin/python
          >>
          >> import BaseConvert
          >> print BaseConvert.con vert(90,BASE10, BASE2)
          >>
          >> Name Error: name 'Base10' is not defined.
          >>
          >> This probably has something to do with namespaces which was biting me
          >> a while ago. I thought that since the 'Base' definitions were global to this
          >> module (BaseConvert.py ) by being defined outside the function (convert),
          >> that when I imported this module, they would be global, too.[/color]
          >
          >in Python, "global" means "belonging to a module", not "visible in all
          >modules in my entire program"
          >[color=green]
          >> What am I still missing?[/color]
          >
          >change the call to use BaseConvert.BAS E10 and BaseConvert.BAS E2
          >
          >to learn more about local and global names, read this:
          >
          > http://www.python.org/doc/current/ref/naming.html
          >
          ></F>[/color]


          Thanks,
          Jeff

          Comment

          • John Roth

            #6
            Re: Base conversion method or module


            "Jeff Wagner" <JWagner@hotmai l.com> wrote in message
            news:jid7tvcfkf 9hhc3vvut4gfbpl 7i1g46t6o@4ax.c om...[color=blue]
            > On Sun, 7 Dec 2003 15:45:41 -0500, "Francis Avila"[/color]
            <francisgavila@ yahoo.com> wrotf:[color=blue]
            >
            >
            > I am getting an error when I import this module and call it.
            >
            > #!/usr/bin/python
            >
            > import BaseConvert
            > print BaseConvert.con vert(90,BASE10, BASE2)
            >
            > Name Error: name 'Base10' is not defined.
            >
            > This probably has something to do with namespaces which was biting me a[/color]
            while ago. I thought that[color=blue]
            > since the 'Base' definitions were global to this module (BaseConvert.py )[/color]
            by being defined outside[color=blue]
            > the function (convert), that when I imported this module, they would be[/color]
            global, too.[color=blue]
            >
            > From one of my books, it says, "An import statement creates a new[/color]
            namespace that contains all the[color=blue]
            > attributes of the module. To access an attribute in this namespace, use[/color]
            the name of the module[color=blue]
            > object as a prefix: import MyModule ... a = MyModule.f( )" which is[/color]
            what I thought I was[color=blue]
            > doing.
            >
            > What am I still missing?[/color]

            print BaseConvert.con vert(90, BaseConvert.BAS E10, BaseConvert.BAS E2)

            John Roth[color=blue]
            >
            > Thanks,
            > Jeff[/color]


            Comment

            • Francis Avila

              #7
              Re: Base conversion method or module


              Jeff Wagner wrote in message ...[color=blue]
              >On Mon, 8 Dec 2003 00:40:46 +0100, "Fredrik Lundh" <fredrik@python ware.com>[/color]
              wrotf:[color=blue]
              >[color=green]
              >>Jeff Wagner wrote:
              >>[color=darkred]
              >>> I found the Python cookbook recipe you were referring to. It is as[/color][/color][/color]
              follows:[color=blue][color=green]
              >>
              >>(what's wrong with just posting an URL?)[/color]
              >
              >What a great idea ;) ...
              >http://aspn.activestate.com/ASPN/Coo.../Recipe/111286[/color]

              Hey, I found another one which is the more general "inverse of int/long"
              function I was pining for (and thus learning for the n-th time that one
              should check the cookbook first before reinventing the wheel):



              For some reason it's in the "Text" category, and uses the term "radix,"
              which is less common than "base".

              Hettinger's version (found in the discussion below) is better.

              Shouldn't something like this get into the builtins, so we can get rid of
              hex/oct in Python3k?
              --
              Francis Avila

              Comment

              • Mensanator

                #8
                Re: Base conversion method or module

                >Subject: Re: Base conversion method or module[color=blue]
                >From: "Francis Avila" francisgavila@y ahoo.com
                >Date: 12/7/2003 8:52 PM Central Standard Time
                >Message-id: <vt7pt71h3bsf78 @corp.supernews .com>
                >
                >
                >Jeff Wagner wrote in message ...[color=green]
                >>On Mon, 8 Dec 2003 00:40:46 +0100, "Fredrik Lundh" <fredrik@python ware.com>[/color]
                >wrotf:[color=green]
                >>[color=darkred]
                >>>Jeff Wagner wrote:
                >>>
                >>>> I found the Python cookbook recipe you were referring to. It is as[/color][/color]
                >follows:[color=green][color=darkred]
                >>>
                >>>(what's wrong with just posting an URL?)[/color]
                >>
                >>What a great idea ;) ...
                >>http://aspn.activestate.com/ASPN/Coo.../Recipe/111286[/color]
                >
                >Hey, I found another one which is the more general "inverse of int/long"
                >function I was pining for (and thus learning for the n-th time that one
                >should check the cookbook first before reinventing the wheel):
                >
                >http://aspn.activestate.com/ASPN/Coo.../Recipe/222109[/color]

                These are nice, but they're very slow:

                For i=2**177149 - 1

                224.797000051 sec for baseconvert
                202.733999968 sec for dec2bin (my routine)
                137.735000014 sec for radix

                Compare those to the .digits function that is part of GMPY

                0.59399998188 sec

                That can make quite a difference when you're running through a couple million
                iterations.

                [color=blue]
                >
                >For some reason it's in the "Text" category, and uses the term "radix,"
                >which is less common than "base".
                >
                >Hettinger's version (found in the discussion below) is better.
                >
                >Shouldn't something like this get into the builtins, so we can get rid of
                >hex/oct in Python3k?
                >--
                >Francis Avila[/color]



                --
                Mensanator
                Ace of Clubs

                Comment

                • Jeff Wagner

                  #9
                  Re: Base conversion method or module

                  On Sun, 7 Dec 2003 21:52:23 -0500, "Francis Avila" <francisgavila@ yahoo.com> wrotf:
                  [color=blue]
                  >
                  >Jeff Wagner wrote in message ...[color=green]
                  >>On Mon, 8 Dec 2003 00:40:46 +0100, "Fredrik Lundh" <fredrik@python ware.com>[/color]
                  >wrotf:[color=green]
                  >>[color=darkred]
                  >>>Jeff Wagner wrote:
                  >>>
                  >>>> I found the Python cookbook recipe you were referring to. It is as[/color][/color]
                  >follows:[color=green][color=darkred]
                  >>>
                  >>>(what's wrong with just posting an URL?)[/color]
                  >>
                  >>What a great idea ;) ...
                  >>http://aspn.activestate.com/ASPN/Coo.../Recipe/111286[/color]
                  >
                  >Hey, I found another one which is the more general "inverse of int/long"
                  >function I was pining for (and thus learning for the n-th time that one
                  >should check the cookbook first before reinventing the wheel):
                  >
                  >http://aspn.activestate.com/ASPN/Coo.../Recipe/222109
                  >
                  >For some reason it's in the "Text" category, and uses the term "radix,"
                  >which is less common than "base".
                  >
                  >Hettinger's version (found in the discussion below) is better.
                  >
                  >Shouldn't something like this get into the builtins, so we can get rid of
                  >hex/oct in Python3k?[/color]

                  Thanks Francis, this is excellent! I think I just made a major breakthrough. It's all starting to
                  come together. I'm sure something will stump me again, though (and it's going to be Classes when I
                  get there ;)

                  Jeff

                  Comment

                  • Jeff Wagner

                    #10
                    Re: Base conversion method or module

                    On 08 Dec 2003 04:45:17 GMT, mensanator@aol. compost (Mensanator) wrotf:
                    [color=blue][color=green]
                    >>Subject: Re: Base conversion method or module
                    >>From: "Francis Avila" francisgavila@y ahoo.com
                    >>Date: 12/7/2003 8:52 PM Central Standard Time
                    >>Message-id: <vt7pt71h3bsf78 @corp.supernews .com>
                    >>
                    >>
                    >>Jeff Wagner wrote in message ...[color=darkred]
                    >>>On Mon, 8 Dec 2003 00:40:46 +0100, "Fredrik Lundh" <fredrik@python ware.com>[/color]
                    >>wrotf:[color=darkred]
                    >>>
                    >>>>Jeff Wagner wrote:
                    >>>>
                    >>>>> I found the Python cookbook recipe you were referring to. It is as[/color]
                    >>follows:[color=darkred]
                    >>>>
                    >>>>(what's wrong with just posting an URL?)
                    >>>
                    >>>What a great idea ;) ...
                    >>>http://aspn.activestate.com/ASPN/Coo.../Recipe/111286[/color]
                    >>
                    >>Hey, I found another one which is the more general "inverse of int/long"
                    >>function I was pining for (and thus learning for the n-th time that one
                    >>should check the cookbook first before reinventing the wheel):
                    >>
                    >>http://aspn.activestate.com/ASPN/Coo.../Recipe/222109[/color]
                    >
                    >These are nice, but they're very slow:
                    >
                    >For i=2**177149 - 1
                    >
                    >224.79700005 1 sec for baseconvert
                    >202.73399996 8 sec for dec2bin (my routine)
                    >137.73500001 4 sec for radix
                    >
                    >Compare those to the .digits function that is part of GMPY
                    >
                    >0.5939999818 8 sec
                    >
                    >That can make quite a difference when you're running through a couple million
                    >iterations.[/color]

                    So I decide to go and try out this GMPY and download the win32 binaries. It consists of two files,
                    gmpy.pyd and pysymbolicext.p yd ... what do I do with them, just copy them to the lib folder?

                    Then it says I need GMP-4.x so I get that, too. It's like nothing I've ever seen before. How can I
                    install that on my WinXP box?

                    Thanks, Jeff

                    Comment

                    • Terry Reedy

                      #11
                      Re: Base conversion method or module


                      "Jeff Wagner" <JWagner@hotmai l.com> wrote in message
                      news:0b88tvo38l 9ca8fhc3127mskq egdpfocn7@4ax.c om...[color=blue]
                      > So I decide to go and try out this GMPY and download the win32 binaries.[/color]
                      It consists of two files,[color=blue]
                      > gmpy.pyd and pysymbolicext.p yd ... what do I do with them, just copy them[/color]
                      to the lib folder?

                      Specifically, Pythonx.y/Libs/site-packages (at least on my system)
                      [color=blue][color=green][color=darkred]
                      >>>import sys; sys.path[/color][/color][/color]

                      should have site-packages dir near beginning.
                      [color=blue]
                      > Then it says I need GMP-4.x so I get that, too. It's like nothing I've[/color]
                      ever seen before. How can I[color=blue]
                      > install that on my WinXP box?[/color]

                      no idea. sorry. .so is unix version of .dll. try gmp site for windows
                      ..dll binary

                      tjr


                      Comment

                      • Mensanator

                        #12
                        Re: Base conversion method or module

                        >Subject: Re: Base conversion method or module[color=blue]
                        >From: Jeff Wagner JWagner@hotmail .com
                        >Date: 12/8/2003 1:01 AM Central Standard Time
                        >Message-id: <0b88tvo38l9ca8 fhc3127mskqegdp focn7@4ax.com>
                        >
                        >On 08 Dec 2003 04:45:17 GMT, mensanator@aol. compost (Mensanator) wrotf:
                        >[color=green][color=darkred]
                        >>>Subject: Re: Base conversion method or module
                        >>>From: "Francis Avila" francisgavila@y ahoo.com
                        >>>Date: 12/7/2003 8:52 PM Central Standard Time
                        >>>Message-id: <vt7pt71h3bsf78 @corp.supernews .com>
                        >>>
                        >>>
                        >>>Jeff Wagner wrote in message ...
                        >>>>On Mon, 8 Dec 2003 00:40:46 +0100, "Fredrik Lundh"[/color][/color]
                        ><fredrik@pytho nware.com>[color=green][color=darkred]
                        >>>wrotf:
                        >>>>
                        >>>>>Jeff Wagner wrote:
                        >>>>>
                        >>>>>> I found the Python cookbook recipe you were referring to. It is as
                        >>>follows:
                        >>>>>
                        >>>>>(what's wrong with just posting an URL?)
                        >>>>
                        >>>>What a great idea ;) ...
                        >>>>http://aspn.activestate.com/ASPN/Coo.../Recipe/111286
                        >>>
                        >>>Hey, I found another one which is the more general "inverse of int/long"
                        >>>function I was pining for (and thus learning for the n-th time that one
                        >>>should check the cookbook first before reinventing the wheel):
                        >>>
                        >>>http://aspn.activestate.com/ASPN/Coo.../Recipe/222109[/color]
                        >>
                        >>These are nice, but they're very slow:
                        >>
                        >>For i=2**177149 - 1
                        >>
                        >>224.7970000 51 sec for baseconvert
                        >>202.7339999 68 sec for dec2bin (my routine)
                        >>137.7350000 14 sec for radix
                        >>
                        >>Compare those to the .digits function that is part of GMPY
                        >>
                        >>0.593999981 88 sec
                        >>
                        >>That can make quite a difference when you're running through a couple[/color]
                        >million[color=green]
                        >>iterations.[/color]
                        >
                        >So I decide to go and try out this GMPY and download the win32 binaries. It
                        >consists of two files,
                        >gmpy.pyd and pysymbolicext.p yd ... what do I do with them, just copy them to
                        >the lib folder?[/color]

                        Yes. Did you get the documentation files also? You'll want gmpydoc.txt as it
                        lists all the functions. Unfortunately, it's a Unix test file (no carraige
                        returns) making it useless for Windows Notepad unless you convert the line
                        feeds to carraige return/line feed.
                        [color=blue]
                        >
                        >Then it says I need GMP-4.x so I get that, too. It's like nothing I've ever
                        >seen before. How can I
                        >install that on my WinXP box?[/color]

                        You only need that if you are going to compile from the source files. The
                        Windows binaries are already compiled, so you shouldn't need to do that step.
                        [color=blue]
                        >
                        >Thanks, Jeff[/color]



                        --
                        Mensanator
                        Ace of Clubs

                        Comment

                        • Jeff Wagner

                          #13
                          Re: Base conversion method or module

                          On 09 Dec 2003 04:57:18 GMT, mensanator@aol. compost (Mensanator) wrotf:
                          [color=blue][color=green]
                          >>So I decide to go and try out this GMPY and download the win32 binaries. It
                          >>consists of two files,
                          >>gmpy.pyd and pysymbolicext.p yd ... what do I do with them, just copy them to
                          >>the lib folder?[/color]
                          >
                          >Yes. Did you get the documentation files also? You'll want gmpydoc.txt as it
                          >lists all the functions. Unfortunately, it's a Unix test file (no carraige
                          >returns) making it useless for Windows Notepad unless you convert the line
                          >feeds to carraige return/line feed.[/color]

                          Ok, I got it, cleaned it up and read it. It's pretty good.
                          [color=blue][color=green]
                          >>Then it says I need GMP-4.x so I get that, too. It's like nothing I've ever
                          >>seen before. How can I
                          >>install that on my WinXP box?[/color]
                          >
                          >You only need that if you are going to compile from the source files. The
                          >Windows binaries are already compiled, so you shouldn't need to do that step.
                          >[/color]

                          Ok, I must have misunderstood. I am trying something that doesn't seem to be working.

                          If I try to take 01011010 (5A) from binary to hex, here is what I get:[color=blue][color=green][color=darkred]
                          >>> gmpy.digits(010 11010,16) #this is not correct.[/color][/color][/color]
                          '0x41208'

                          From binary to decimal doesn't work either.[color=blue][color=green][color=darkred]
                          >>> gmpy.digits(010 11010,10)[/color][/color][/color]
                          '266760'

                          If I go from decimal to hex, it works.[color=blue][color=green][color=darkred]
                          >>> gmpy.digits(90, 16)[/color][/color][/color]
                          '0x5a'

                          From decimal to binary seems to work ok.[color=blue][color=green][color=darkred]
                          >>> gmpy.digits(90, 2)[/color][/color][/color]
                          '1011010'

                          From hex to binary seems to work.[color=blue][color=green][color=darkred]
                          >>> gmpy.digits(0x5 a,2)[/color][/color][/color]
                          '1011010'

                          I''m not sure why the binary to x doesn't seem to work.

                          Jeff

                          Comment

                          • Andrew Bennetts

                            #14
                            Re: Base conversion method or module

                            On Tue, Dec 09, 2003 at 06:35:34AM +0000, Jeff Wagner wrote:[color=blue]
                            >
                            > Ok, I must have misunderstood. I am trying something that doesn't seem to be working.
                            >
                            > If I try to take 01011010 (5A) from binary to hex, here is what I get:[color=green][color=darkred]
                            > >>> gmpy.digits(010 11010,16) #this is not correct.[/color][/color]
                            > '0x41208'
                            >[color=green]
                            > >From binary to decimal doesn't work either.[color=darkred]
                            > >>> gmpy.digits(010 11010,10)[/color][/color]
                            > '266760'[/color]

                            In Python, integer literals beginning with '0' are in octal, just as how
                            literals beginning with '0x' are in hex, so 01011010 really is 266760:
                            [color=blue][color=green][color=darkred]
                            >>> print 01011010[/color][/color][/color]
                            266760

                            So it looks like gmpy is functioning correctly.

                            -Andrew.


                            Comment

                            • Jeff Wagner

                              #15
                              Re: Base conversion method or module

                              On Tue, 9 Dec 2003 17:45:20 +1100, Andrew Bennetts <andrew-pythonlist@puzz ling.org> wrotf:
                              [color=blue]
                              >On Tue, Dec 09, 2003 at 06:35:34AM +0000, Jeff Wagner wrote:[color=green]
                              >>
                              >> Ok, I must have misunderstood. I am trying something that doesn't seem to be working.
                              >>
                              >> If I try to take 01011010 (5A) from binary to hex, here is what I get:[color=darkred]
                              >> >>> gmpy.digits(010 11010,16) #this is not correct.[/color]
                              >> '0x41208'
                              >>[color=darkred]
                              >> >From binary to decimal doesn't work either.
                              >> >>> gmpy.digits(010 11010,10)[/color]
                              >> '266760'[/color]
                              >
                              >In Python, integer literals beginning with '0' are in octal, just as how
                              >literals beginning with '0x' are in hex, so 01011010 really is 266760:
                              >[color=green][color=darkred]
                              >>>> print 01011010[/color][/color]
                              >266760
                              >
                              >So it looks like gmpy is functioning correctly.
                              >
                              >-Andrew.
                              >[/color]

                              Ok, so I get rid of the leading 0 and here is what I get:
                              [color=blue][color=green][color=darkred]
                              >>> gmpy.digits(101 1010,10)[/color][/color][/color]
                              '1011010'

                              why? How do I tell gmpy that 1011010 is binary and I want it to convert to decimal or hex?

                              Jeff

                              Comment

                              Working...