You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python在二进制文件中写入编码的十六进制值

Writing Hex Values to a Binary File in Python

Hey there! Let's walk through exactly how to write hex values (both specific ones and random ones) to a binary file called stack.bin using Python. Binary files deal with raw byte data, so we just need to convert those hex values into proper byte objects first.

Writing Specific Hex Values (0x77, 0xff, 0x11)

First, let's handle the specific values you mentioned: 0x77, 0xff, and 0x11. Each of these is a single-byte hex value (they range from 0x00 to 0xff, which maps to decimal 0-255), so we can convert them directly into a byte string using Python's bytes() function.

Here's the code:

# Define the specific hex values as integers (0x prefix denotes hex)
specified_values = [0x77, 0xff, 0x11]

# Open the file in write-binary mode ('wb' overwrites existing content)
with open('stack.bin', 'wb') as file:
    # Convert the integer list to bytes and write to the file
    file.write(bytes(specified_values))

The with statement ensures the file is automatically closed after writing, which is safer than manually calling file.close().

Writing Random Hex Values

To generate random hex values, we'll use Python's random module to create random integers between 0 and 255 (since that's the full range of a single byte). We can then convert these integers to bytes just like before.

Here's how to write N random hex values (adjust the count to your needs):

import random

# Number of random bytes to generate
random_byte_count = 10  # Change this to any number you want

# Generate random integers (0-255) and convert to bytes
random_bytes = bytes(random.randint(0, 255) for _ in range(random_byte_count))

# Write to stack.bin (overwrites existing content)
with open('stack.bin', 'wb') as file:
    file.write(random_bytes)

Combining Both: Specific + Random Values

If you want to write the specific values first, followed by random ones (or vice versa), just chain the write() calls inside the same file context:

import random

# Specific values
specified_values = [0x77, 0xff, 0x11]
# Random values (5 in this example)
random_bytes = bytes(random.randint(0, 255) for _ in range(5))

# Write both to the file
with open('stack.bin', 'wb') as file:
    file.write(bytes(specified_values))
    file.write(random_bytes)

Quick Notes to Keep in Mind

  • Use 'wb' mode to overwrite the file, or 'ab' mode if you want to append new bytes to the end of an existing file instead of replacing it.
  • Make sure all your hex values are single-byte (0x00 to 0xff). If you try to pass an integer larger than 255 to bytes(), you'll get a ValueError.
  • To verify the file content, you can use tools like hexdump (on Linux/macOS) or a hex editor (like HxD on Windows) to check that the bytes match what you wrote.

内容的提问来源于stack exchange,提问作者Sumit Somani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 20:52:29