# How to convert base 10 to base 2?

Discussion in 'Python' started by gianpycea@gmail.com, Aug 20, 2012.

1. ### Guest

Hi,
as you can argue from the subject, i'm really,really new to python.
What is the best way to achieve that with python? Because the syntax int('30',2) doesn't seem to work!

, Aug 20, 2012

2. ### Benjamin KaplanGuest

On Mon, Aug 20, 2012 at 12:50 AM, <> wrote:
> Hi,
> as you can argue from the subject, i'm really,really new to python.
> What is the best way to achieve that with python? Because the syntax int('30',2) doesn't seem to work!

That syntax goes the other way- from a string representing a number in
the given base into an integer (which doesn't have a base, although
it's stored in base 2).

You get the binary by doing bin(x), where x is an integer.

>>> bin(12)

'0b1100'

If you want to go from a string in base-10 to a string in base-2, you
have to do the conversion to integer first.

>>> bin(int('5',10))

'0b101'

Although you don't need to specify the 10 because that's the default.
>>> bin(int('5'))

'0b101'

Benjamin Kaplan, Aug 20, 2012

3. ### Mark LawrenceGuest

On 20/08/2012 08:50, wrote:
> Hi,
> as you can argue from the subject, i'm really,really new to python.
> What is the best way to achieve that with python? Because the syntax int('30',2) doesn't seem to work!
>

When you have a problem please cut and paste the exact thing that you
tried with the entire traceback. "Doesn't seem to work" doesn't tell us
a lot. Your OS and Python version is usually needed as well but we
might be able to skip those details in this case How much general
programming experience do you have?

--
Cheers.

Mark Lawrence.

Mark Lawrence, Aug 20, 2012
4. ### Guest

On Monday, August 20, 2012 9:50:53 AM UTC+2, (unknown) wrote:
> Hi,
>
> as you can argue from the subject, i'm really,really new to python.
>
> What is the best way to achieve that with python? Because the syntax int('30',2) doesn't seem to work!

Thank you all for the big help!
@Mark Lawrence
Yes, you're definetely right: i should have posted my OS and the version but being a very rough question on syntax i thought it didn't really matter.
I've quite a good general programming experience. I know Java,Visual Basic.NET,Pascal and Mathematica.

, Aug 20, 2012
5. ### Jean-Michel PichavantGuest

wrote:
> On Monday, August 20, 2012 9:50:53 AM UTC+2, (unknown) wrote:
>
>> Hi,
>>
>> as you can argue from the subject, i'm really,really new to python.
>>
>> What is the best way to achieve that with python? Because the syntax int('30',2) doesn't seem to work!
>>

>
> Thank you all for the big help!
> @Mark Lawrence
> Yes, you're definetely right: i should have posted my OS and the version but being a very rough question on syntax i thought it didn't really matter.
> I've quite a good general programming experience. I know Java,Visual Basic.NET,Pascal and Mathematica.
>

note that the builtin bin function is not available with python ver < 2.6

def Denary2Binary(n):
'''convert denary integer n to binary string bStr'''
bStr = ''
if n < 0: raise ValueError, "must be a positive integer"
if n == 0: return '0'
while n > 0:
bStr = str(n % 2) + bStr
n = n >> 1
return bStr

JM

(not my function but I can't remember who I stole from)

Jean-Michel Pichavant, Aug 20, 2012
6. ### Dennis Lee BieberGuest

On Mon, 20 Aug 2012 16:52:42 +0200, Jean-Michel Pichavant
<> declaimed the following in
gmane.comp.python.general:

> note that the builtin bin function is not available with python ver < 2.6
>
> def Denary2Binary(n):
> '''convert denary integer n to binary string bStr'''
> bStr = ''
> if n < 0: raise ValueError, "must be a positive integer"
> if n == 0: return '0'
> while n > 0:
> bStr = str(n % 2) + bStr
> n = n >> 1
> return bStr
>
> JM
>
> (not my function but I can't remember who I stole from)

I think I typically have done this by going through a hex
representation.

H2B_Lookup = { "0" : "0000", "1" : "0001",
"2" : "0010", "3" : "0011",
"4" : "0100", "5" : "0101",
"6" : "0110", "7" : "0111",
"8" : "1000", "9" : "1001",
"A" : "1010", "B" : "1011",
"C" : "1100", "D" : "1101",
"D" : "1110", "F" : "1111" }

def I2B(i):
sgn = " "
if i < 0:
sgn = "-"
i = -i
h = ("%X" % i)
return sgn + "".join([H2B_Lookup[c] for c in h])

>>> from i2b import I2B
>>> I2B(10)

' 1010'
>>> I2B(1238)

' 010011100110'
>>> I2B(-6)

'-0110'
>>>

--
Wulfraed Dennis Lee Bieber AF6VN
HTTP://wlfraed.home.netcom.com/

Dennis Lee Bieber, Aug 20, 2012
7. ### Joel GoldstickGuest

On Mon, Aug 20, 2012 at 1:29 PM, Dennis Lee Bieber
<> wrote:
> On Mon, 20 Aug 2012 16:52:42 +0200, Jean-Michel Pichavant
> <> declaimed the following in
> gmane.comp.python.general:
>
>> note that the builtin bin function is not available with python ver < 2.6
>>
>> def Denary2Binary(n):
>> '''convert denary integer n to binary string bStr'''
>> bStr = ''
>> if n < 0: raise ValueError, "must be a positive integer"
>> if n == 0: return '0'
>> while n > 0:
>> bStr = str(n % 2) + bStr
>> n = n >> 1
>> return bStr
>>
>> JM
>>
>> (not my function but I can't remember who I stole from)

>
> I think I typically have done this by going through a hex
> representation.
>
> H2B_Lookup = { "0" : "0000", "1" : "0001",
> "2" : "0010", "3" : "0011",
> "4" : "0100", "5" : "0101",
> "6" : "0110", "7" : "0111",
> "8" : "1000", "9" : "1001",
> "A" : "1010", "B" : "1011",
> "C" : "1100", "D" : "1101",
> "D" : "1110", "F" : "1111" }
>
> def I2B(i):
> sgn = " "
> if i < 0:
> sgn = "-"
> i = -i
> h = ("%X" % i)
> return sgn + "".join([H2B_Lookup[c] for c in h])
>
>>>> from i2b import I2B
>>>> I2B(10)

> ' 1010'
>>>> I2B(1238)

> ' 010011100110'
>>>> I2B(-6)

> '-0110'
>>>>

> --
> Wulfraed Dennis Lee Bieber AF6VN
> HTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list

This may be moving off topic, but since you encode -6 as -0110 I
thought I'd chime in on 'two's complement'

with binary number, you can represent 0 to 255 in a byte, or you can
represent numbers from 127 to -128. To get the negative you
complement each bit (0s to 1s, 1s to 0s), then add one to the result.
So:
3 --> 00000011
~3 -> 111111100
result 111111101

fine with a mixture of negative and positive numbers.

eg 8 + (-3) ----> 00001000
111111101
gives: 00000101
which is 5!

--
Joel Goldstick

Joel Goldstick, Aug 20, 2012
8. ### Ian KellyGuest

On Mon, Aug 20, 2012 at 11:57 AM, Joel Goldstick
<> wrote:
> This may be moving off topic, but since you encode -6 as -0110 I
> thought I'd chime in on 'two's complement'
>
> with binary number, you can represent 0 to 255 in a byte, or you can
> represent numbers from 127 to -128. To get the negative you
> complement each bit (0s to 1s, 1s to 0s), then add one to the result.
> So:
> 3 --> 00000011
> ~3 -> 111111100
> result 111111101
>
> fine with a mixture of negative and positive numbers.
>
> eg 8 + (-3) ----> 00001000
> 111111101
> gives: 00000101
> which is 5!

The main reason to use two's complement is when you need to encode the
negative sign of the number as a bit in a format of fixed width, e.g.
within a byte or word. In a string representation, unless you are
specifically trying to represent computer memory, it is usually better
to just use a minus sign, to be more consistent with how we usually
represent numerals.

Otherwise, note that complement representations are not specific to
binary. For example, with decimal numbers we could use "ten's
complement": individually subtract each digit from 9, and add 1 to the
result. A leading digit between 5 and 9 would be considered negative.

So, for example:
-(042) --> 958
-(958) --> 042

958 + 042 == 000

Cheers,
Ian

Ian Kelly, Aug 20, 2012
9. ### Paul RubinGuest

Ian Kelly <> writes:
> Everybody should know the generic algorithm, though:
> from itertools import chain ...

For n>0, assuming you just want the converted digits and not a string.
String conversion and minus sign for n<0 left as exercise. Note this
returns a generator that you can convert to a list with list(...).

def convert(n, base):
a,b = divmod(n,base)
if a > 0:
for e in convert(a,base):
yield e
yield b

Paul Rubin, Aug 21, 2012
10. ### Miki TebekaGuest

> You get the binary by doing bin(x), where x is an integer.
Note that Python also support binary number literals (prefixed with 0b):
In [1]: 0b101
Out[1]: 5

Miki Tebeka, Aug 21, 2012
11. ### Miki TebekaGuest

> You get the binary by doing bin(x), where x is an integer.
Note that Python also support binary number literals (prefixed with 0b):
In [1]: 0b101
Out[1]: 5

Miki Tebeka, Aug 21, 2012