decimal to binary

J

John Hunter

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


http://www.python.org/doc/current/lib/module-struct.html

John Hunter
 
M

Manish Jethani

manuel said:
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
 
P

Peter Hansen

manuel said:
How to convert a decimal to binary ?

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
 
G

Gary Herron

How to convert a decimal to binary ?

thx,

Manuel

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
 
M

manuel

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:
http://www.kino3d.com/forum/files/test4.jpg

and this is the discussion:
http://www.elysiun.com/forum/viewtopic.php?t=13135&postdays=0&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
fileReaded = f.read();
msgstring = "Parsing tga..."; Draw()
for i in range(len(fileReaded)):
listByte.append(ord(fileReaded))
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
 
M

Manish Jethani

manuel said:
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(fileReaded)):
listByte.append(ord(fileReaded))
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(*@'
instead of
[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
 
C

Christopher Koppler

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 = 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
 
M

manuel

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

Manish Jethani

manuel said:
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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top