decimal to binary

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

    decimal to binary

    How to convert a decimal to binary ?

    thx,

    Manuel


  • John Hunter

    #2
    Re: decimal to binary

    >>>>> "manuel" == manuel <manuelbastioni NOSPAM@tin.it> writes:

    manuel> How to convert a decimal to binary ? thx,

    1. Divide the "desired" base (in this case base 2) INTO the
    number you are trying to convert.

    2. Write the quotient (the answer) with a remainder like you did
    in elementary school.

    3. Repeat this division process using the whole number from the
    previous quotient (the number in front of the remainder).

    4. Continue repeating this division until the number in front of
    the remainder is only zero.

    5. The answer is the remainders read from the bottom up.

    Is this what you meant :-)? Or do you want to convert an integer to a
    binary string in python? If the latter, see the struct module




    John Hunter

    Comment

    • Peter Hansen

      #3
      Re: decimal to binary

      manuel wrote:[color=blue]
      >
      > How to convert a decimal to binary ?[/color]

      No builtin function, but you could likely find lots of previous
      questions like this, and answers.

      If you don't feel like searching, can you post an example of
      what you want in and what you want out? Some people asking
      this kind of question are confused about representations
      of numbers and how data is really stored in computers, and
      they don't really want what they thing they want.

      A quick Google groups search with "convert decimal binary"
      reveals answers that would probably work for you.

      -Peter

      Comment

      • Manish Jethani

        #4
        Re: decimal to binary

        manuel wrote:
        [color=blue]
        > How to convert a decimal to binary ?[/color]

        def foo(i):
        b = ''
        while i > 0:
        j = i & 1
        b = str(j) + b
        i >>= 1
        return b

        Maybe this is inefficient, but...

        -Manish

        --
        Manish Jethani (manish.j at gmx.net)
        phone (work) +91-80-51073488

        Comment

        • Gary Herron

          #5
          Re: decimal to binary

          On Friday 25 July 2003 05:21 am, manuel wrote:[color=blue]
          > How to convert a decimal to binary ?
          >
          > thx,
          >
          > Manuel[/color]

          We probably need more information than that to answer your question.
          For instance statement

          x = 123

          converts the decimal number represented by the decimal digits 123 into
          binary form and stores it in variable x. Of course you have no access
          to the binary digits as

          print x

          converts back to decimal to print "123"

          So what is it you really want?

          Gary Herron



          Comment

          • manuel

            #6
            Re: decimal to binary

            > We probably need more information than that to answer your question.

            I'm writing a script for Blender to make a true displacement
            feature. This is a sample:


            and this is the discussion:

            start=0


            Please, excuse me for my poor english, it's very
            hard for me to explain a development question,
            because I'm italian, and I've little time to study
            other language...Real ly I prefer study 3D and python. :-P
            The main problem with newsgroup it that
            after send the message I can't modify it... :-(

            This is the first time that I try to handle
            binary file.

            I read a tga file, and store all bytes in a list:

            ---------------------------------------
            #LETTURA DEL FILE TGA
            listByte = []
            try:
            f = open(filename,' rb')
            except IOError,(errno, strerror):
            msgstring = "I/O error(%s): %s" % (errno, strerror); Draw()
            return
            fileReaded = f.read();
            msgstring = "Parsing tga..."; Draw()
            for i in range(len(fileR eaded)):
            listByte.append (ord(fileReaded[i]))
            f.close()
            ------------------------------------------

            I use ord() to convert the value of byte in
            integer, if I don't make this, I've the ASCII
            symbol...

            But, for listByte[17], I must read the sequence
            of 0 and 1, because I must know the 4° and 5° bit:
            this bits specify the image origin.

            How I can read the 4° and 5° bit from listByte[17]?

            Thanks,

            Manuel Bastioni






            Comment

            • Manish Jethani

              #7
              Re: decimal to binary

              manuel wrote:
              [color=blue]
              > This is the first time that I try to handle
              > binary file.
              >
              > I read a tga file, and store all bytes in a list:
              >
              > ---------------------------------------
              > #LETTURA DEL FILE TGA
              > listByte = []
              > try:
              > f = open(filename,' rb')
              > except IOError,(errno, strerror):
              > msgstring = "I/O error(%s): %s" % (errno, strerror); Draw()
              > return
              > fileReaded = f.read();
              > msgstring = "Parsing tga..."; Draw()
              > for i in range(len(fileR eaded)):
              > listByte.append (ord(fileReaded[i]))
              > f.close()
              > ------------------------------------------
              >
              > I use ord() to convert the value of byte in
              > integer, if I don't make this, I've the ASCII
              > symbol...[/color]

              You CAN store the byte as it is, in a string. So, for example,
              you can store
              'a(*@'
              instead of
              [97, 40, 42, 64]
              But it depends on your requirements.
              [color=blue]
              > But, for listByte[17], I must read the sequence
              > of 0 and 1, because I must know the 4° and 5° bit:
              > this bits specify the image origin.[/color]

              This function returns the binary representation as a string:

              def foo(i):
              b = ''
              while i > 0:
              j = i & 1
              b = str(j) + b
              i >>= 1
              return b

              bin = foo(listByte[17])
              bin[-4] # 4th (3rd) bit
              bin[-5] # 5th (4th) bit

              You have to check for IndexError, or check the length of the string.

              BUT! But you don't need to do this in order to get the value of
              the 4th (3rd) bit. Just do this:

              if listByte[17] & (1 << 3):
              <bit is set>
              else:
              <bit is not set>

              For the 5th (4th) bit, change '<< 3' to '<< 4'; and so on.

              -Manish

              --
              Manish Jethani (manish.j at gmx.net)
              phone (work) +91-80-51073488

              Comment

              • Christopher Koppler

                #8
                Re: decimal to binary

                On Fri, 25 Jul 2003 18:52:00 GMT, "manuel"
                <manuelbastioni NOSPAM@tin.it> wrote:
                [color=blue]
                >I use ord() to convert the value of byte in
                >integer, if I don't make this, I've the ASCII
                >symbol...
                >
                >But, for listByte[17], I must read the sequence
                >of 0 and 1, because I must know the 4° and 5° bit:
                >this bits specify the image origin.
                >
                >How I can read the 4° and 5° bit from listByte[17]?[/color]


                You can use this function to generate a list of digits (least
                significant first!) from any integer, with the default being the 8
                binary digits of one byte (and no checking for anything):

                def digitlist(value , numdigits=8, base=2):
                val = value
                digits = [0 for i in range(numdigits )]
                for i in range(numdigits ):
                val, digits[i] = divmod(val, base)
                return digits
                [color=blue][color=green][color=darkred]
                >>> digitlist(234) # binary 11101010[/color][/color][/color]
                [0, 1, 0, 1, 0, 1, 1, 1][color=blue][color=green][color=darkred]
                >>> digitlist(39) # binary 00100111[/color][/color][/color]
                [1, 1, 1, 0, 0, 1, 0, 0]

                Now you can easily access the 4th and 5th elements (which are probably
                reversed if you needed the 4th and 5th bit most significant first)...

                Hope this helps,
                Christopher

                --
                Christopher

                Comment

                • manuel

                  #9
                  Re: decimal to binary

                  > if listByte[17] & (1 << 3):


                  Thanks!

                  But I don't understand completely the meaning of
                  & (1 << 3)

                  Can you suggest me a page in python on line manual,
                  or a tutorial?




                  Comment

                  • Manish Jethani

                    #10
                    Re: decimal to binary

                    manuel wrote:
                    [color=blue][color=green]
                    >>if listByte[17] & (1 << 3):[/color]
                    >
                    >
                    >
                    > Thanks!
                    >
                    > But I don't understand completely the meaning of
                    > & (1 << 3)[/color]

                    (1 << 3) will left-shift 1 by 3 bits

                    00000000 00000000 00000000 00000001

                    giving you 8

                    00000000 00000000 00000000 00001000

                    Then you AND your number (25, for example) with 8

                    00000000 00000000 00000000 00011001
                    00000000 00000000 00000000 00001000
                    -----------------------------------
                    00000000 00000000 00000000 00001000

                    if you get a non-zero value, then the bit is set.

                    If your number is 23

                    00000000 00000000 00000000 00010111
                    00000000 00000000 00000000 00001000
                    -----------------------------------
                    00000000 00000000 00000000 00000000

                    then the result of the AND is 0, and that means the bit is not set.
                    [color=blue]
                    > Can you suggest me a page in python on line manual,
                    > or a tutorial?[/color]

                    See the section on bit-wise operations in the Python Reference
                    Manual.

                    -Manish

                    --
                    Manish Jethani (manish.j at gmx.net)
                    phone (work) +91-80-51073488

                    Comment

                    Working...