您可以使用切片(或str.rstrip),int和format的组合.

>>> 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'), 16), 'b')

'1000000010000000100000001000000010'

更新:

from itertools import islice

def formatted_bin(inp):

output = format(int(inp.rstrip('UL'), 16), '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打印二进制内容_在python中打印出c类型的二进制表示相关推荐

  1. python打印换行符_在Python编程中不使用换行符进行打印

    python打印换行符 In different programming languages such as C, C++, Java, etc. by default, the printing s ...

  2. python 打印数组变量_使用Python将数组的元素导出到变量中(unpacking)

    下面就为大家分享一篇使用Python将数组的元素导出到变量中(unpacking),具有很好的参考价值,希望对大家有所帮助.一起过来看看吧 最近工作中遇到一个问题,需要利用Python将数组(list ...

  3. python 打印皮卡丘_用python打印你的宠物小精灵吧

    我们来通过一个有趣的例子开始编写我们的第一个python代码. 本文涉及的python基础语法为:print输出函数,赋值,字符串 print() print()是python的一个内置函数,用于打印 ...

  4. python打印数字倒三角形_在python中用while语句打印出倒三角形【python 三角形】

    在python中用while语句打印出倒三角形 i = int(input("shuru")) a = 0 while a b = 0 while b print(end=&quo ...

  5. python更新excel内容_使用python将Excel表中的数据更新到数据库中

    代码如下: # -*- coding: utf-8 -*- import pymysql import xlrd # 连接数据库 try: db = pymysql.connect(host=&quo ...

  6. python网络编程内容_图解Python网络编程

    Python Python开发 Python语言 图解Python网络编程 本篇索引 (1)基本原理 本篇指的网络编程,仅仅是指如何在两台或多台计算机之间,通过网络收发数据包:而不涉及具体的应用层功能 ...

  7. python 读取邮件内容_利用Python imaplib和email模块 读取邮件文本内容及附件内容...

    python使用imap接收邮件的过程探索 https://www.cnblogs.com/yhlx/archive/2013/03/22/2975817.html #! encoding:utf8 ...

  8. python 打印自己代码_用python打印网页源代码

    我刚刚在使用Python3.2.5的Win7上尝试了同样的方法,下面是我得到的:Python 3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 ...

  9. python删除excel内容_用python批量删除sheet

    因为工作关系维护了一个Excel文件,格式是xlsx,每天一个sheet,从17年下半年到目前已经有上百个sheet,导致文件越来越大,发邮件加载也变得很慢.想要删除一部分,保留近一个月的,当然手动删 ...

最新文章

  1. 使用非侵入性脑机接口和计算机视觉引导对机器人手臂进行共享控制
  2. 随笔 —— 当下不晚
  3. linux shell 脚本 2,理解Linux Shell和基本的Shell脚本(2)
  4. Pandas.plot 做图 demo(scatter,bar,pie)
  5. 创建型模式---工厂模式
  6. 建议收藏,详细篇 PyCharm 完美教程
  7. C++工作笔记-调试release版的dll
  8. 解决An attempt was made to load a program with an incorrect format.问题
  9. 细数魅族metal电信版手机3宗罪。
  10. Docker Yearning + Inception SQL审核平台搭建
  11. 04---Net基础加强
  12. eclipse报错Server Tomcat v7.0 Server at localhost failed to start.
  13. bl wn810a linux驱动下载,BL-LW06-AR/BL-WN810A无线网卡驱动for all【支持所有系统】
  14. 4k纸是几厘米乘几厘米_4k纸有多大长多少宽多少
  15. 动环监控系统中B接口的实现
  16. ubuntu修改ssh端口_在Ubuntu上更改SSH欢迎横幅
  17. OLTP 和 OLAP 的区别?
  18. Android 程序员必须掌握的三种自动化测试方法
  19. Shannon理论——笔记1
  20. online-section1-new

热门文章

  1. cas java单点登录_java单点登录系统CAS的简单使用
  2. 正则表达式的运算符优先级
  3. C语言运算符的优先级及结合性
  4. git安装 perl ubuntu_ubuntu下安装git
  5. matlab mat文件 太大,MATLAB .mat文件中的开销过大
  6. mysql 导入导出大文件
  7. android 启动优化方案,Android 项目优化(五):应用启动优化
  8. python zipfile setpassword_Python模块:zipfile
  9. go struct结构
  10. IOS_OC_id ,NSObject, idlt;NSObjectgt;差别