How to display a byte array as hex values
2016-07-11 18:21
603 查看
How
to display a byte array as hex values
up vote2down votefavorite | >>> struct.pack('2I',12, 30) b'\x0c\x00\x00\x00\x1e\x00\x00\x00' >>> struct.pack('2I',12, 31) b'\x0c\x00\x00\x00\x1f\x00\x00\x00' >>> struct.pack('2I',12, 32) b'\x0c\x00\x00\x00 \x00\x00\x00' ^in question >>> struct.pack('2I',12, 33) b'\x0c\x00\x00\x00!\x00\x00\x00' ^in question I'd like all values to display as hex python
| ||||||||||||||||
a comment |
3 Answers
activeoldestvotesup vote4down voteaccepted | How about his?>>> data = struct.pack('2I',12, 30) >>> [hex(ord(c)) for c in data] ['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0'] The expression [item for item in sequence]is a so called list comprehension. It's basically a very compact way of writing a simple forloop, and creating a list from the result. The ord()builtin function takes a string, and turns it into an integer that's its corresponding unicode code point (for characters in the ASCII character set that's the same as their value in the ASCII table). Its counterpart, chr()for 8bit strings or unichr()for unicode objects do the opposite. The hex()builtin then simply converts the integers into their hex representation. As pointed out by @TimPeters, in Python 3 you would need to lose the ord(), because iterating over a bytes object will (already) yield integers: Python 3.4.0a3 (default, Nov 8 2013, 18:33:56) >>> import struct >>> data = struct.pack('2I',12, 30) >>> type(data) <class 'bytes'> >>> type(data[1]) <class 'int'> >>> >>> [hex(i) for i in data] ['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']
| ||||||||||||||||
a comment |
up vote5down vote | You have to reformat it yourself if you want \xescapes everywhere; e.g., >>> import struct >>> r = struct.pack('2I',12, 33) >>> r b'\x0c\x00\x00\x00!\x00\x00\x00' >>> list(r) [12, 0, 0, 0, 33, 0, 0, 0] >>> print("".join("\\x%02x" % i for i in r)) \x0c\x00\x00\x00\x21\x00\x00\x00
| ||||||||
a comment |
up vote2down vote | Try binascii.hexlify: >>> import binascii >>> import struct >>> binascii.hexlify(struct.pack('2I',12,30)) b'0c0000001e000000' >>> binascii.hexlify(struct.pack('2I',12,31)) b'0c0000001f000000' >>> binascii.hexlify(struct.pack('2I',12,32)) b'0c00000020000000' >>> binascii.hexlify(struct.pack('2I',12,33)) b'0c00000021000000' Or if you want spaces to make it more readable: >>> ' '.join(format(n,'02X') for n in struct.pack('2I',12,33)) '0C 00 00 00 21 00 00 00' |
相关文章推荐
- GUPImage库的队列管理
- BigBlueButton安装过程(在虚拟机中)
- UE4学习笔记(3)——World Composition无缝拼接地图实现
- iOS8 的毛玻璃效果(UIVisualEffectView)
- 【SSH网上商城】Incorrect datetime value: '' for column 'ordertime' at row 1
- JUC源码分析20-队列-DelayQueue
- 安卓6.0需要BLUETOOTH_PRIVILEGED的大坑
- 使用delayedQueue实现你本地的延迟队列
- vue.js数据绑定语法
- Android Volley完全解析(三),定制自己的Request
- Your build settings specify a provisioning profile with the UUID, no provisioning profile was found
- HDU 4116 Fruit Ninja
- iOS摄像头和相册-UIImagePickerController-浅析
- iOS开发学习之利用系统远程UITableView 自定义滑动删除按钮、增加自定义滑动按钮方法
- 十大滤波算法程序大全(Arduino精编无错版)
- Juery 获得select的值
- UIKIT详解之UILabel全面解析
- CSS: Multiple Attribute Selector [name="value"][name2="value2"]
- db2 uuid
- UIDynamic 入门基础