Defining how a sequence of bytes sits in a memory buffer or on disk can be challenging from time to time. Since everything that you’ll work with is a byte, it makes sense that we have an intuitive way to work with this information agnostic of the overlying type restrictions that the language will enforce on us.

In today’s post, I’m going to run through Python’s byte string packing and unpacking using the struct package.

Basics

From the Python documentation:

This module performs conversions between Python values and C structs represented as Python bytes objects. This can be used in handling binary data stored in files or from network connections, among other sources. It uses Format Strings as compact descriptions of the layout of the C structs and the intended conversion to/from Python values.

When working with a byte string in Python, you prefix your literals with b.

  1. >>> b'Hello'
  2. 'Hello'

The ord function call is used to convert a text character into its character code representation.

  1. >>> ord(b'H')
  2. 72
  3. >>> ord(b'e')
  4. 101
  5. >>> ord(b'l')
  6. 108

We can use list to convert a whole string of byte literals into an array.

  1. >>> list(b'Hello')
  2. [72, 101, 108, 108, 111]

The compliment to the ord call is chr, which converts the byte-value back into a character.

Packing

Using the struct module, we’re offered the pack function call. This function takes in a format of data and then the data itself. The first parameter defines how the data supplied in the second parameter should be laid out. We get started:

  1. >>> import struct

If we pack the string 'Hello' as single bytes:

  1. >>> list(b'Hello')
  2. [72, 101, 108, 108, 111]
  3. >>> struct.pack(b'BBBBB', 72, 101, 108, 108, 111)
  4. b'Hello'

The format string b'BBBBB' tells pack to pack the values supplied into a string of 5 unsigned values. If we were to use a lower case b in our format string, pack would expect the byte value to be signed.

  1. >>> struct.pack(b'bbbbb', 72, 101, 108, 108, 111)
  2. b'Hello'

This only gets interesting once we send a value that would make the request overflow:

  1. >>> struct.pack(b'bbbbb', 72, 101, 108, 129, 111)
  1. Traceback (most recent call last):
  2. File "<stdin>", line 1, in <module>
  3. struct.error: byte format requires -128 <= number <= 127

The following tables have been re-produced from the Python documentation.

Byte order, size and alignment

Character Byte order Size Alignment
@ native native native
= native standard none
< little-endian standard none
> big-endian standard none
! network (= big-endian) standard none

Types

Format C Type Python type Standard size Notes
x pad byte no value    
c char bytes of length 1 1  
b signed char integer 1 (1),(3)
B unsigned char integer 1 (3)
? _Bool bool 1 (1)
h short integer 2 (3)
H unsigned short integer 2 (3)
i int integer 4 (3)
I unsigned int integer 4 (3)
l long integer 4 (3)
L unsigned long integer 4 (3)
q long long integer 8 (2), (3)
Q unsigned long long integer 8 (2), (3)
n ssize_t integer   (4)
N size_t integer   (4)
f float float 4 (5)
d double float 8 (5)
s char[] bytes    
p char[] bytes    
P void * integer   (6)

Unpacking

The direct reverse process of packing bytes into an array, is unpacking them again into usable variables inside of your python code.

  1. >>> struct.unpack(b'BBBBB', struct.pack(b'BBBBB', 72, 101, 108, 108, 111))
  2. (72, 101, 108, 108, 111)
  3. >>> struct.unpack(b'5s', struct.pack(b'BBBBB', 72, 101, 108, 108, 111))
  4. (b'Hello',)

Packing data with Python的更多相关文章

  1. 使用Python对Twitter进行数据挖掘(Mining Twitter Data with Python)

    目录 1.Collecting data 1.1 Register Your App 1.2 Accessing the Data 1.3 Streaming 2.Text Pre-processin ...

  2. python data analysis | python数据预处理(基于scikit-learn模块)

    原文:http://www.jianshu.com/p/94516a58314d Dataset transformations| 数据转换 Combining estimators|组合学习器 Fe ...

  3. Mining Twitter Data with Python

    目录 1.Collecting data 1.1 Register Your App 1.2 Accessing the Data 1.3 Streaming 2.Text Pre-processin ...

  4. Working with Binary Data in Python

    http://www.devdungeon.com/content/working-binary-data-python

  5. 7 Tools for Data Visualization in R, Python, and Julia

    7 Tools for Data Visualization in R, Python, and Julia Last week, some examples of creating visualiz ...

  6. 一句Python,一句R︱pandas模块——高级版data.frame

    先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. pandas可谓如雷贯耳,数据处理神器. 以下符号: = ...

  7. Python - 2. Built-in Collection Data Types

    From: http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.htm ...

  8. A Complete Tutorial to Learn Data Science with Python from Scratch

    A Complete Tutorial to Learn Data Science with Python from Scratch Introduction It happened few year ...

  9. python接口测试(post,get)-传参(data和json之间的区别)

    python接口测试如何正确传参: POST 传data:data是python字典格式:传参data=json.dumps(data)是字符串类型传参 #!/usr/bin/env python3 ...

随机推荐

  1. 2019_西湖论剑_预选赛 testre

    2019_西湖论剑_预选赛 testre 程序中关键操作是比较ptr,其中夹杂的一部分v26计算是为了混淆我们的分析.那么我们只要跟踪ptr数组的生成便可,向上发现v11,加密操作数组. 接下来跟踪v ...

  2. 13 条高效实用的 JavaScript 单行代码

    JavaScript可以实现很多令人惊奇的事! 从复杂的框架到处理API,有太多的东西可以学习. 甚至,仅用一行代码,它也能完成一些很棒的工作. 不信?那么请看这13条JavaScript单行代码,用 ...

  3. XML数据持久化学习笔记

    一.XML基础语法 1.XML结构:XML是一种树结构的文本 2.XML注释:格式:<!--在其中书写注释-->,在注释中可以单行注释也可以多行注释 3.固定内容:<?xml ver ...

  4. Android学习中出现的问题

    •问题1:多行文字如何实现跑马灯效果? 博客链接:Androidd Studio 之多行文字跑马灯特效 解决状态:已解决 •问题2:cause: unable to find valid certif ...

  5. Nginx日志分析 awk 命令

    通过Nginx日志,可以简单分析WEB网站的运行状态.数据报表.IP.UV(unique visitor指独立访客访问数,一台电脑终端为一个访客.).PV(page view即页面访问量,每打开一次页 ...

  6. Istio安全-证书管理(实操一)

    Istio安全-证书管理 目录 Istio安全-证书管理 插入现有CA证书 插入现有证书和密钥 部署Istio 配置示例services 校验证书 卸载 Istio的DNS证书管理 DNS证书的提供和 ...

  7. 手摸手教你阅读和调试大型开源项目 ZooKeeper

    本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费开源.有趣.入门级的 ZooKeeper 教程,面向有编程基础的新手. 项 ...

  8. [图论]最优布线问题:kruskal

    最优布线问题 目录 最优布线问题 Description Input Output Sample Input Sample Output Hint 解析 代码 Description 学校有n台计算机 ...

  9. day-01-初识Python与条件判断

    cpu 内存 硬盘 操作系统 cpu:计算机的运算和计算中心,相当于人类大脑.飞机 ​ 内存:暂时存储数据,临时加载数据应用程序,4G,8G,16G,32G ​ 速度快,高铁,断电即消失.造价很高 ​ ...

  10. 【DB宝48】JumpServer:多云环境下更好用的堡垒机

    目录 一.JumpServer简介 1.1.页面展示 1.2.特色优势 1.3.功能列表 1.4.架构图 1.5.端口说明 1.6.产品组件 二.安装JumpServer 2.1.一键自动部署 2.2 ...