概述
能否请你帮忙?
>>> inp = '0x0202020202UL' >>> format(int(inp[:-2],16),'b') '1000000010000000100000001000000010' # Using `str.rstrip`,This will work for any hex,not just UL >>> format(int(inp.rstrip('UL'),'b') '1000000010000000100000001000000010'
更新:
from itertools import islice def formatted_bin(inp): output = format(int(inp.rstrip('UL'),'b') le = len(output) m = le % 4 padd = 4 - m if m != 0 else 0 output = output.zfill(le + padd) it = iter(output) return ' '.join(''.join(islice(it,4)) for _ in xrange((le+padd)/4)) print formatted_bin('0x0202020202UL') print formatted_bin('0x10') print formatted_bin('0x101010') print formatted_bin('0xfff')
输出:
0010 0000 0010 0000 0010 0000 0010 0000 0010 0001 0000 0001 0000 0001 0000 0001 0000 1111 1111 1111
总结
以上是编程之家为你收集整理的在python中打印出c类型的二进制表示全部内容,希望文章能够帮你解决在python中打印出c类型的二进制表示所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧