如何将数字转换为二进制? [英] How to convert a number to binary?
问题描述
将二进制转换为基数10很简单:
>> int(' '11111111'',2)
255
将基数10转换为十六进制或八进制很简单:
>> oct(100)
''0144''
>> hex(100)
''0x64''
有没有* easy *方法将数字转换为二进制?
2007年5月17日下午6:33,Lyosha写道:
将二进制转换为b ase 10很简单:
>>> int(''11111111'',2)
255
将基数10转换为十六进制或八进制很简单:
>>> oct(100)
''0144''
>>> hex(100)
''0x64''
有没有* easy *方法将数字转换为二进制?
def to_base(数字,基数):
''将基数10整数转换为另一个基数''
>
number = int(number)
base = int(base)
如果base< 2或基数36:
引发ValueError,Base必须介于2和36之间。
如果不是数字:
返回0
symbols = string.digits + string.lowercase [:26]
回答= []
而数字:
数字,余数= divmod(数字,基数)
answer.append(符号[余数])
返回''''。join(反向(回答))
希望这有帮助,
迈克尔
---
我宁愿使用Java而不是Perl。而且我宁愿被一条比使用Java还要好的b $ b b鳄鱼吃掉。 ? Trouser
5月17日下午4:40,Michael Bentley< mich ... @ jedimindworks.comwrote:
2007年5月17日下午6:33,Lyosha写道:
将二进制转换为基数10很容易:
>> int(''11111111'',2)
255
将基数10转换为十六进制或八进制很简单:
>> oct(100)
''0144''
>> hex(100)
''0x64''
是否有* easy *方法将数字转换为二进制?
def to_base(数字,基数):
''将基数10整数转换为另一个基数''
>
number = int(number)
base = int(base)
如果base< 2或基数36:
提高ValueError,基数必须在2到36之间
如果不是数字:
返回0
symbols = string.digits + string.lowercase [:26]
answer = []
而数字:
数字,余数= divmod(数字,基数)
answer.append(符号[余数])
返回''''。join(反转(回答) ))
希望这会有所帮助,
Michael
这太复杂了...有没有什么方法可以将它转换为一个-B
班轮,以便我能记住它?我非常难看:
"" .join(str((n / base ** i)%base)i在范围(20)中,如果n> = base ** i)
[:: - 1] .zfill(1)
5月17日下午6:45,Lyosha< lyos ... @ gmail.comwrote:
5月17日下午4:40,Michael Bentley< mich ... @ jedimindworks.comwrote:
2007年5月17日下午6:33,Lyosha写道:
将二进制转换为基数10很简单:
>>> int(''11111111'',2)
255
将基数10转换为十六进制或八进制很容易:
>>> oct(100)
''0144''
>>> hex(100)
''0x64''
有没有* easy *方法将数字转换为二进制?
def to_base(number,base):
''将基数10整数转换为另一个基数' '
number = int(number)
base = int(base)
if基数< 2或基数36:
提高ValueError,基数必须在2到36之间
如果不是数字:
返回0
symbols = string.digits + string.lowercase [:26]
answer = []
虽然数字:
数字,余数= divmod(数字,基数)
answer.append(符号[余数])
返回'' ''。join(反向(回答))
希望这会有所帮助,
Michael
这太复杂了......有没有什么方法可以将它转换为一个-B / B
班轮,这样我才能记住它?我非常难看:
"" .join(str((n / base ** i)%base)i在范围(20)中,如果n> = base ** i)
[:: - 1] .zfill(1) -
获取gmpy模块(注意涉及八进制和十六进制的不一致):
>> import gmpy
for base in xrange(2,37): print gmpy.digits(255,base)
11111111
100110
3333
2010
1103
513
0377
313
255
212
193
168
143
120
0xff
f0
e3
d8
cf
c3
bd
b2
af
a5
9l
9c
93
8n
8f
87
7v
7o
7h
7a
73
Converting binary to base 10 is easy:
>>int(''11111111'', 2)
255
Converting base 10 number to hex or octal is easy:
>>oct(100)
''0144''
>>hex(100)
''0x64''
Is there an *easy* way to convert a number to binary?
On May 17, 2007, at 6:33 PM, Lyosha wrote:
Converting binary to base 10 is easy:>>>int(''11111111'', 2)
255
Converting base 10 number to hex or octal is easy:>>>oct(100)
''0144''
>>>hex(100)
''0x64''
Is there an *easy* way to convert a number to binary?
def to_base(number, base):
''converts base 10 integer to another base''
number = int(number)
base = int(base)
if base < 2 or base 36:
raise ValueError, "Base must be between 2 and 36"
if not number:
return 0
symbols = string.digits + string.lowercase[:26]
answer = []
while number:
number, remainder = divmod(number, base)
answer.append(symbols[remainder])
return ''''.join(reversed(answer))
Hope this helps,
Michael
---
"I would rather use Java than Perl. And I''d rather be eaten by a
crocodile than use Java." ? Trouser
On May 17, 4:40 pm, Michael Bentley <mich...@jedimindworks.comwrote:On May 17, 2007, at 6:33 PM, Lyosha wrote:
Converting binary to base 10 is easy:>>int(''11111111'', 2)255
Converting base 10 number to hex or octal is easy:>>oct(100)''0144''
>>hex(100)''0x64''
Is there an *easy* way to convert a number to binary?
def to_base(number, base):
''converts base 10 integer to another base''
number = int(number)
base = int(base)
if base < 2 or base 36:
raise ValueError, "Base must be between 2 and 36"
if not number:
return 0
symbols = string.digits + string.lowercase[:26]
answer = []
while number:
number, remainder = divmod(number, base)
answer.append(symbols[remainder])
return ''''.join(reversed(answer))
Hope this helps,
MichaelThat''s way too complicated... Is there any way to convert it to a one-
liner so that I can remember it? Mine is quite ugly:
"".join(str((n/base**i) % base) for i in range(20) if n>=base**i)
[::-1].zfill(1)
On May 17, 6:45 pm, Lyosha <lyos...@gmail.comwrote:On May 17, 4:40 pm, Michael Bentley <mich...@jedimindworks.comwrote:
On May 17, 2007, at 6:33 PM, Lyosha wrote:
Converting binary to base 10 is easy:
>>>int(''11111111'', 2)
255
Converting base 10 number to hex or octal is easy:
>>>oct(100)
''0144''
>>>hex(100)
''0x64''
Is there an *easy* way to convert a number to binary?
def to_base(number, base):
''converts base 10 integer to another base''
number = int(number)
base = int(base)
if base < 2 or base 36:
raise ValueError, "Base must be between 2 and 36"
if not number:
return 0
symbols = string.digits + string.lowercase[:26]
answer = []
while number:
number, remainder = divmod(number, base)
answer.append(symbols[remainder])
return ''''.join(reversed(answer))
Hope this helps,
Michael
That''s way too complicated... Is there any way to convert it to a one-
liner so that I can remember it? Mine is quite ugly:
"".join(str((n/base**i) % base) for i in range(20) if n>=base**i)
[::-1].zfill(1)-Get the gmpy module (note inconsistencies involving octal and hex):
>>import gmpy
for base in xrange(2,37): print gmpy.digits(255,base)
11111111
100110
3333
2010
1103
513
0377
313
255
212
193
168
143
120
0xff
f0
e3
d8
cf
c3
bd
b2
af
a5
9l
9c
93
8n
8f
87
7v
7o
7h
7a
73
这篇关于如何将数字转换为二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!