杰瑞科技汇

bytearray python

Of course! Let's dive deep into bytearray() in Python.

bytearray python-图1
(图片来源网络,侵删)

What is bytearray()?

bytearray() is a built-in Python function that creates a mutable (changeable) sequence of bytes. Think of it as a flexible list where each element is an integer between 0 and 255 (representing a byte).

It's the mutable counterpart to the immutable bytes type.


Key Characteristics

  1. Mutable: You can change, add, or remove elements from a bytearray after it's created. This is its most important feature.
  2. Sequence of Integers: It's an ordered collection of integers in the range 0 <= x < 256.
  3. Similar to list: You can use many of the same methods you'd use on a list, like append(), insert(), pop(), and slicing.
  4. No String Methods: Because it's a sequence of numbers, not characters, you cannot use string methods like .upper(), .split(), or .find() on it directly.

Creating a bytearray

You can create a bytearray in several ways:

From an Iterable of Integers

You can pass any iterable (like a list or tuple) of integers, as long as each integer is between 0 and 255.

bytearray python-图2
(图片来源网络,侵删)
# From a list of integers
data = bytearray([72, 101, 108, 108, 111])  # ASCII for "Hello"
print(data)        # Output: bytearray(b'Hello')
print(list(data))  # Output: [72, 101, 108, 108, 111]

From a String (Encoding)

This is a very common use case. You create a bytearray from a string by specifying an encoding (like 'utf-8' or 'ascii').

# From a string with an encoding
message = "Hello, World!"
data = bytearray(message, 'utf-8')
print(data)        # Output: bytearray(b'Hello, World!')
print(list(data))  # Output: [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

From an Existing bytes Object

You can convert an immutable bytes object into a mutable bytearray.

# From a bytes object
bytes_data = b"Python"
byte_data = bytearray(bytes_data)
print(byte_data) # Output: bytearray(b'Python')

With an Integer (Creates Null Bytes)

If you pass a single integer n, it creates a bytearray of size n filled with null bytes (\x00, which is integer 0).

# Creates a bytearray of 5 null bytes
data = bytearray(5)
print(data) # Output: bytearray(b'\x00\x00\x00\x00\x00')

Common Operations (Mutability in Action)

Here’s how you can modify a bytearray, which you cannot do with a regular bytes object.

bytearray python-图3
(图片来源网络,侵删)
# Start with a bytearray
ba = bytearray(b'Hello, World!')
# 1. Change an item using index assignment (like a list)
ba[0] = 74  # J is ASCII 74
print(ba) # Output: bytearray(b'Jello, World!')
# 2. Slice and assign (changes multiple bytes)
ba[7:12] = b'Python'
print(ba) # Output: bytearray(b'Jello, Python!')
# 3. Append a single integer
ba.append(33) # 33 is the ASCII code for '!'
print(ba) # Output: bytearray(b'Jello, Python!!')
# 4. Extend with an iterable of integers
ba.extend([32, 84, 104, 97, 110, 107, 115]) # ASCII for " Thanks"
print(ba) # Output: bytearray(b'Jello, Python!! Thanks')
# 5. Pop an item
last_byte = ba.pop()
print(f"Popped byte: {last_byte}") # Output: Popped byte: 115
print(ba) # Output: bytearray(b'Jello, Python!! Thank')

bytearray vs. bytes vs. str

This is the most crucial distinction to understand.

Feature str (String) bytes bytearray
Purpose Text data (Unicode characters) Binary data (immutable) Binary data (mutable)
Mutability Immutable Immutable Mutable
Elements Unicode characters (e.g., 'A', 'é', '🚀') Integers in 0-255 Integers in 0-255
Literal 'hello' b'hello' Created with bytearray()
Methods .upper(), .split(), .find(), etc. .hex(), .decode() Similar to list (append, pop)
Encoding N/A (is the decoded form) .decode() -> str .decode() -> str
Decoding .encode() -> bytes N/A (is the encoded form) .encode() -> bytes

The Conversion Flow

You often convert between these types when dealing with I/O (files, network sockets).

      .encode()
str --------------> bytes
 (Text)        (Immutable Binary)
      .decode()
bytes --------------> str
 (Immutable Binary)   (Text)
      .decode()
bytearray --------------> str
 (Mutable Binary)      (Text)
      .encode()
str --------------> bytearray
 (Text)        (Mutable Binary)

When to Use bytearray()?

Use bytearray() when you need to modify binary data in place. This is common in scenarios like:

  1. Network Programming: Building or parsing network packets where you need to set specific header fields.
  2. File I/O: Reading a binary file into a mutable buffer to modify its contents before writing it back.
  3. Low-Level Data Manipulation: When working with binary formats like images, audio files, or compression algorithms where you need to manipulate individual bytes.
  4. Efficient Appending: If you are building a large binary piece of data piece by piece, bytearray.append() can be more efficient than concatenating immutable bytes objects.

Example: Modifying a Network Packet Header

Let's simulate creating a simple TCP-like packet. The first two bytes might be the "source port" and the next two the "destination port".

# Create a packet with placeholder ports
# Source Port: 1234, Destination Port: 5678
# 1234 in hex is 0x04D2, 5678 is 0x162E
packet = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
# Set the source port (bytes 0 and 1)
packet[0] = 0x04
packet[1] = 0xD2
# Set the destination port (bytes 2 and 3)
packet[2] = 0x16
packet[3] = 0x2E
print(f"Raw Packet Bytes: {packet}")
# Output: Raw Packet Bytes: bytearray(b'\x04\xd2\x16.\x00\x00\x00\x00\x00\x00')
# You can now send this packet over the network

Summary

Feature bytearray()
What it is A mutable sequence of bytes.
Key Use Case When you need to modify binary data.
Common Creation bytearray(string, 'utf-8'), bytearray(iterable_of_ints).
Main Advantage Mutability (.append(), .pop(), slice assignment).
Main Limitation Not suitable for text operations; must be decoded first.
分享:
扫描分享到社交APP
上一篇
下一篇