【说站】python字节数组如何使用?
2023-06-13 09:13:23 时间
python字节数组如何使用?
1、bytes和bytearray的要素都是在0-255之间的整数,但任何字符串都可以通过字符编码方案存储。字节数组切片或相应的字节数组;字节组可以直接显示ASCII字符。
s = 'helloè?í'
b_arr = bytes(s, 'utf_8')
print(type(b_arr))
print(type(b_arr))
for b in b_arr:
print(b, end=' ')
print()
print('element of bytes is int number', b_arr[0])
print('splice of bytes is bytes',end = ' ' )
b_arr_splice = b_arr[:1]
print(b_arr_splice)
num_b_arr = bytes([299])
2、struct模块提供了将打包的字节序列转换节序列转换成由不同类型字段组成的元组,也有一些函数用于反向转换,将元组转换成打包的字节序列。该模块可以处理bytes,bytearray和memoryview对象。
import struct
record_format = 'hd4s'
pack_bytes = struct.pack(record_format, 7 , 3.14,b'gbye')
print(type(pack_bytes))
print(pack_bytes)
with open('struct.b', 'wb') as fp:
fp.write(pack_bytes)
record_size = struct.calcsize(record_format)
with open('struct.b', 'rb') as fp:
record_bs = fp.read(record_size)
print(struct.unpack(record_format, record_bs))
以上就是python字节数组的使用,希望对大家有所帮助。
相关文章
- 二级Python选择题_二级python选择题题库
- python re.compile() 详解——Python正则表达式「建议收藏」
- pycharm中python版本_如何在pycharm中切换python版本「建议收藏」
- python中矩阵的转置怎么写_Python 矩阵转置的几种方法小结
- python 生成数组_Python创建数组「建议收藏」
- python fileinput_Python之fileinput模块学习「建议收藏」
- python删除首行_Python删除文件第一行
- Python入门系列(五)一篇搞懂python语句
- python数组拼接字符串_Python练习题——数组拼接
- python socket recvfrom_Python:socket.recvfrom()不返回任何地址
- 【说站】python如何删除字符串的特殊字符
- 【说站】Python单向循环链表的创建
- 【说站】python列表有几种切片形式
- 【说站】python线程阻塞的解决
- lambda表达式python_Python中的Lambda表达式「建议收藏」
- 数据导入与预处理-第4章-数据获取python读取docx文档
- Python列表常用的函数和方法(3)_Python自学第二十二节
- Python 计算与伪造TCP序列号
- Python 实现DNS查询放大攻击
- Python 字符串与字节数组转换
- 100天学会python
- java和python 编写贪吃蛇游戏
- 小白的Python之路 day1 变量详解编程语言
- Linux系统快速运行Python脚本(linux运行python脚本)
- burpsuite扩展开发之Python
- Python中的jqueryPyQuery库使用小结
- Python中的类学习笔记