Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   decimal to binary (http://www.velocityreviews.com/forums/t320117-decimal-to-binary.html)

 manuel 07-25-2003 12:21 PM

decimal to binary

How to convert a decimal to binary ?

thx,

Manuel

 John Hunter 07-25-2003 12:37 PM

Re: decimal to binary

>>>>> "manuel" == manuel <manuelbastioniNOSPAM@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.

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

http://www.python.org/doc/current/li...le-struct.html

John Hunter

 Manish Jethani 07-25-2003 12:58 PM

Re: decimal to binary

manuel wrote:

> How to convert a decimal to binary ?

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

 Peter Hansen 07-25-2003 01:01 PM

Re: decimal to binary

manuel wrote:
>
> How to convert a decimal to binary ?

No builtin function, but you could likely find lots of previous

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

 Gary Herron 07-25-2003 01:55 PM

Re: decimal to binary

On Friday 25 July 2003 05:21 am, manuel wrote:
> How to convert a decimal to binary ?
>
> thx,
>
> Manuel

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

 manuel 07-25-2003 06:52 PM

Re: decimal to binary

I'm writing a script for Blender to make a true displacement
feature. This is a sample:
http://www.kino3d.com/forum/files/test4.jpg

and this is the discussion:
http://www.elysiun.com/forum/viewtop...postorder=asc&
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...Really 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
msgstring = "Parsing tga..."; Draw()
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

 Manish Jethani 07-25-2003 08:43 PM

Re: decimal to binary

manuel wrote:

> 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
> msgstring = "Parsing tga..."; Draw()
> f.close()
> ------------------------------------------
>
> I use ord() to convert the value of byte in
> integer, if I don't make this, I've the ASCII
> symbol...

You CAN store the byte as it is, in a string. So, for example,
you can store
'a(*@'
[97, 40, 42, 64]
But it depends on your requirements.

> 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.

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

 Christopher Koppler 07-25-2003 09:11 PM

Re: decimal to binary

On Fri, 25 Jul 2003 18:52:00 GMT, "manuel"
<manuelbastioniNOSPAM@tin.it> wrote:

>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]?

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

>>> digitlist(234) # binary 11101010

[0, 1, 0, 1, 0, 1, 1, 1]
>>> digitlist(39) # binary 00100111

[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

 manuel 07-25-2003 10:54 PM

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?

 Manish Jethani 07-26-2003 12:25 AM

Re: decimal to binary

manuel wrote:

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

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

(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.

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.

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

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

 All times are GMT. The time now is 06:37 PM.