Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python byte array

Shulou Source: shulou.com Published: 2022-05-31 17:43:52 09月20日 Update

This article mainly introduces the relevant knowledge of how to use the python byte array, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the python byte array. Let's take a look at it.

1. The elements of bytes and bytearray are integers between 0255and 255, but any string can be stored in a character encoding scheme. Byte array slices or corresponding byte arrays; byte groups can display ASCII characters directly.

S = 'Helio è re í' 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

2. The struct module provides to convert the packaged byte sequence conversion section sequence into tuples composed of different types of fields, and there are also some functions for reverse conversion to convert the tuples into packaged byte sequences. This module can handle bytes,bytearray and memoryview objects.

Import structrecord_format = 'hd4s'pack_bytes = struct.pack (record_format, 7,3.14) 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)) this is the end of the article on "how to use python byte arrays" Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use python byte arrays". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Bytes arrays characters sequences knowledge content modules articles different between values functions can be passed fields strings objects simple operations integers articles schemes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi Microsoft Shulou Technology NVidia