Binary numbers

  • Thread starter Douglas Soares de Andrade
  • Start date
D

Douglas Soares de Andrade

Hi !

How to work with binary numbers in python ? Is there a way to print a number
in its binary form like we do with oct() or hex() ?

Im doing a project that i have to work with binaries and i tired of convert
numbers to string all the time to perform some operations.

I searched about it in many places like python.org and google, but not found
anything useful.

Thats why im asking this.

And another question... if python has not a way to do this, why i let me use
oct(), hex() and not bin() ?

Thanks for the help !
 
D

Douglas Soares de Andrade

Hi !

Pardon me, but what itoa has to do it the history ?

See ya !

Em Quarta 08 Junho 2005 00:34, Dan Bishop escreveu:
 
D

Dan Bishop

Douglas said:
Hi !

How to work with binary numbers in python ? Is there a way to print a number
in its binary form like we do with oct() or hex() ?

Im doing a project that i have to work with binaries and i tired of convert
numbers to string all the time to perform some operations.

I searched about it in many places like python.org and google, but not found
anything useful.

Search for "itoa".
 
M

mensanator

Douglas said:
Hi !

How to work with binary numbers in python ? Is there a way to print a number
in its binary form like we do with oct() or hex() ?

Im doing a project that i have to work with binaries and i tired of convert
numbers to string all the time to perform some operations.

I searched about it in many places like python.org and google, but not found
anything useful.

Thats why im asking this.

And another question... if python has not a way to do this, why i let me use
oct(), hex() and not bin() ?

Thanks for the help !

--
Douglas Soares de Andrade
http://douglasandrade.cjb.net - dsa at unilestemg.br
UnilesteMG - www.unilestemg.br
ICQ, MSN = 76277921, douglas at tuxfamily.org

You might want to try the GMPY module:
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 '-'
11111111111111111111111111111111
 
D

Douglas Soares de Andrade

Hi!

Em Quarta 08 Junho 2005 09:38, Guyon Morée escreveu:
Don't know if this is what you mean, but:

Binary to decimal:

267

Dont know this way of using it. Thanks for the teachings :)

See ya !
 
?

=?iso-8859-1?B?R3V5b24gTW9y6WU=?=

Don't know if this is what you mean, but:

Binary to decimal:
.... if dec_number == 0: return '0'
.... return (dec2bin(dec_number >> 1) + str(dec_number % 2))
....'0100001011'
 

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
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top