Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27472 > unrolled thread
| Started by | gianpycea@gmail.com |
|---|---|
| First post | 2012-08-20 00:50 -0700 |
| Last post | 2012-08-20 12:10 -0600 |
| Articles | 13 — 10 participants |
Back to article view | Back to comp.lang.python
How to convert base 10 to base 2? gianpycea@gmail.com - 2012-08-20 00:50 -0700
Re: How to convert base 10 to base 2? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-08-20 01:04 -0700
Re: How to convert base 10 to base 2? Miki Tebeka <miki.tebeka@gmail.com> - 2012-08-21 09:23 -0700
Re: How to convert base 10 to base 2? Miki Tebeka <miki.tebeka@gmail.com> - 2012-08-21 09:23 -0700
Re: How to convert base 10 to base 2? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-20 09:10 +0100
Re: How to convert base 10 to base 2? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-20 09:26 +0100
Re: How to convert base 10 to base 2? gianpycea@gmail.com - 2012-08-20 01:57 -0700
Re: How to convert base 10 to base 2? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-20 16:52 +0200
Re: How to convert base 10 to base 2? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-20 13:29 -0400
Re: How to convert base 10 to base 2? Joel Goldstick <joel.goldstick@gmail.com> - 2012-08-20 13:57 -0400
Re: How to convert base 10 to base 2? Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-20 12:00 -0600
Re: How to convert base 10 to base 2? Paul Rubin <no.email@nospam.invalid> - 2012-08-20 21:05 -0700
Re: How to convert base 10 to base 2? Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-20 12:10 -0600
| From | gianpycea@gmail.com |
|---|---|
| Date | 2012-08-20 00:50 -0700 |
| Subject | How to convert base 10 to base 2? |
| Message-ID | <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | Benjamin Kaplan <benjamin.kaplan@case.edu> |
|---|---|
| Date | 2012-08-20 01:04 -0700 |
| Message-ID | <mailman.3544.1345449933.4697.python-list@python.org> |
| In reply to | #27472 |
On Mon, Aug 20, 2012 at 12:50 AM, <gianpycea@gmail.com> 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'
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-08-21 09:23 -0700 |
| Message-ID | <5fb03f8e-e910-4d86-8e34-a7561385ab88@googlegroups.com> |
| In reply to | #27473 |
> 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
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-08-21 09:23 -0700 |
| Message-ID | <mailman.3605.1345566183.4697.python-list@python.org> |
| In reply to | #27473 |
> 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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2012-08-20 09:10 +0100 |
| Message-ID | <mailman.3545.1345449995.4697.python-list@python.org> |
| In reply to | #27472 |
On 20/08/2012 08:50, gianpycea@gmail.com 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.
[toc] | [prev] | [next] | [standalone]
| From | lipska the kat <lipskathekat@yahoo.co.uk> |
|---|---|
| Date | 2012-08-20 09:26 +0100 |
| Message-ID | <9q2dncu3dbomaazNnZ2dnUVZ8jKdnZ2d@bt.com> |
| In reply to | #27472 |
On 20/08/12 08:50, gianpycea@gmail.com 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
>>> x = bin(30)[2:]
>>> x
'11110'
>>> int(x, 2)
30
>>>
lipska
--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
[toc] | [prev] | [next] | [standalone]
| From | gianpycea@gmail.com |
|---|---|
| Date | 2012-08-20 01:57 -0700 |
| Message-ID | <28b3f583-fad1-46da-a6b5-933f966eb401@googlegroups.com> |
| In reply to | #27472 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| Date | 2012-08-20 16:52 +0200 |
| Message-ID | <mailman.3560.1345474364.4697.python-list@python.org> |
| In reply to | #27478 |
gianpycea@gmail.com 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)
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2012-08-20 13:29 -0400 |
| Message-ID | <mailman.3569.1345483790.4697.python-list@python.org> |
| In reply to | #27478 |
On Mon, 20 Aug 2012 16:52:42 +0200, Jean-Michel Pichavant
<jeanmichel@sequans.com> 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
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2012-08-20 13:57 -0400 |
| Message-ID | <mailman.3571.1345485481.4697.python-list@python.org> |
| In reply to | #27478 |
On Mon, Aug 20, 2012 at 1:29 PM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
> On Mon, 20 Aug 2012 16:52:42 +0200, Jean-Michel Pichavant
> <jeanmichel@sequans.com> 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
> wlfraed@ix.netcom.com 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
add 1 1
result 111111101
The nice thing about this representation is that arithmetic works just
fine with a mixture of negative and positive numbers.
eg 8 + (-3) ----> 00001000
111111101
gives: 00000101
which is 5!
--
Joel Goldstick
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-08-20 12:00 -0600 |
| Message-ID | <mailman.3573.1345485660.4697.python-list@python.org> |
| In reply to | #27478 |
On Mon, Aug 20, 2012 at 11:29 AM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
> 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'
>>>>
I would throw a .lstrip('0') in there to get rid of the ugly leading
zeroes (and also add a special case for i == 0).
Everybody should know the generic algorithm, though:
from itertools import chain
def convert(n, base):
digits = [chr(x) for x in chain(range(ord('0'), ord('9')+1),
range(ord('A'), ord('Z')+1))]
if not 2 <= base <= len(digits):
raise ValueError("invalid base")
output = []
sign = ""
if n < 0:
n = -n
sign = "-"
while n > 0:
n, r = divmod(n, base)
output.append(digits[r])
return sign + ''.join(reversed(output)) or '0'
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-08-20 21:05 -0700 |
| Message-ID | <7xpq6kga92.fsf@ruckus.brouhaha.com> |
| In reply to | #27517 |
Ian Kelly <ian.g.kelly@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-08-20 12:10 -0600 |
| Message-ID | <mailman.3574.1345486250.4697.python-list@python.org> |
| In reply to | #27478 |
On Mon, Aug 20, 2012 at 11:57 AM, Joel Goldstick <joel.goldstick@gmail.com> 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 > add 1 1 > result 111111101 > > The nice thing about this representation is that arithmetic works just > 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web