Python与Node.js Base64解码差异及struct.unpack适配问题求助
Hey there! Let's clear up the confusion and get your struct parsing working smoothly.
First, let's get one key point straight: the bytes object returned by base64.standard_b64decode is exactly the same raw byte data as Node.js's Buffer—they just look different when printed. Python shows printable ASCII characters as-is (like g, E, s) and non-printable ones as escape sequences (like \x00, \xef), while Node.js displays every byte as its two-digit hex value. But under the hood, the byte sequence is identical.
You don't need to use binascii.hexlify at all for parsing—you can work directly with the decoded bytes object using struct.unpack. Here's how to do it step by step:
Step 1: Keep the Raw Decoded Bytes
Skip the hexlify step entirely. Start with the decoded bytes from base64:
import base64 import struct decoded_data = base64.standard_b64decode("AO/Nq4lnRSMBZXMnLHcKXhSObYxiFvY=")
Step 2: Define Your Packet Structure
Let's say your MQTT message has a header with specific byte formats (adjust this to match your actual packet spec). For example, if the header is:
- 1 byte: unsigned char (packet type)
- 4 bytes: big-endian 32-bit integer (payload length)
You'd define the struct format string like this:
# ">B I" means big-endian (>) followed by unsigned char (B) and unsigned int (I) header_format = ">B I"
Step 3: Split and Parse the Bytes
Use Python's slice notation to split the header from the payload, then unpack it with struct:
# Calculate how many bytes the header takes up header_length = struct.calcsize(header_format) # Split header and payload header_bytes = decoded_data[:header_length] payload_bytes = decoded_data[header_length:] # Parse the header packet_type, payload_length = struct.unpack(header_format, header_bytes) # Now you can work with the parsed values and payload print(f"Packet Type: {packet_type}") print(f"Payload Length: {payload_length}") print(f"Payload Data: {payload_bytes}")
Debugging Tip: View Hex Values (If Needed)
If you want to verify the byte values match Node.js's output for debugging, you can convert the bytes to a hex string (no need for binascii):
hex_string = decoded_data.hex() print(hex_string) # Output: 00efcdab89674523016573272c770a5e148e6d8c6216f6
Why This Works
The struct module in Python operates directly on raw bytes—those \x00 escape sequences are just Python's way of displaying non-printable bytes, not part of the actual data. When you pass decoded_data to struct.unpack, it reads the underlying byte values correctly, just like Node.js would with its Buffer.
内容的提问来源于stack exchange,提问作者Sharath Chandra

