Python Integer Byte Code Conversion Question

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

import struct

test_struct = struct.pack('B', 34)
print("test_tmp : ", test_struct)

test_bytes = bytes([34])
print("test convert bytes : ", test_bytes)

>>>
test_tmp :  b'"'
test convert bytes :  b'"'

When the integers from 32 to approximately 150 change to byte code, they do not change to the same form as b'\x53' but to a strange form. How do I make the byte code conversion run properly?

python convert int

2022-09-20 11:35

1 Answers

The expression is only shown as a character corresponding to the ASCII value (" has an ASCII value of 0x22 = 34). It seems to be exactly converted.

Save as file (with open ("binary_dump.bin", "wb") as f: f.Try writing(test_bytes) and open it with a hexaditor. You'll be able to see what's saved as you want.


2022-09-20 11:35

If you have any answers or tips


© 2022 pinfo. All rights reserved.