自己编写? 类似于: def bin( x): 来自数学导入日志 bits =(8如果x == 0则8 *(int(log(x)/ log(2))/ 8 +1)) 返回''''。join([str((x> i)& 1)for i in range(bits)[:: - 1]]) 虽然这可能很慢:/ - 使用Opera革命性的电子邮件客户端: http://www.opera.com/mail/ Problem:how to get binary from integer and vice versa?The simplest way I know is:a = 0100a64 but:a = 100 (I want binary number)does not work that way. a.__hex__ existsa.__oct__ exists but where is a.__bin__ ???What`s the simplest way to do this?Thank you very much. 解决方案 Here''s a sketch; I''ll leave you to fill in the details -- you may wishto guard against interesting input like b < 2. .... tmp = [].... while n:.... n, d = divmod(n, b).... tmp.append(digits[d]).... return ''''.join(reversed(tmp)).... ''1234'' ''111'' [''1000000'', ''2101'', ''1000'', ''224'', ''144'', ''121'', ''100'', ''71'', ''64'',''59'', ''54'', ''4c'', ''48'', ''44'', ''40''] HTH,John Here''s a sketch; I''ll leave you to fill in the details -- you may wishto guard against interesting input like b < 2. .... tmp = [].... while n:.... n, d = divmod(n, b).... tmp.append(digits[d]).... return ''''.join(reversed(tmp)).... ''1234'' ''111'' [''1000000'', ''2101'', ''1000'', ''224'', ''144'', ''121'', ''100'', ''71'', ''64'',''59'', ''54'', ''4c'', ''48'', ''44'', ''40''] HTH,John Write your own? something like: def bin(x):from math import logbits = (8 if x == 0 else 8*(int(log(x)/log(2))/8+1))return ''''.join([str((x >i) & 1) for i in range(bits)[::-1]]) though that''s probably slow :/-- Using Opera''s revolutionary e-mail client: http://www.opera.com/mail/ 这篇关于一个= 0100;打印; 64如何扭转这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 09:01