1

I am trying to use f.write(struct.pack()) to write n bytes to a binary file but not quite sure how to do that? Any example or sample would be helpful.

Ravs
  • 261
  • 2
  • 7
  • 15
  • It's not clear what you mean when you say that you want to "write bytes" or that your data "is HEX". Please show the code that produces the data you want to write, and explain what you are hoping `struct.pack` will do for you. – Karl Knechtel Oct 31 '19 at 01:16
  • BTW, Python 2.x will officially no longer be supported as of the end of this year. – Karl Knechtel Oct 31 '19 at 01:17

2 Answers2

1

You don't really explain your exact problem or what you tried and which error messages you encountered:

The solution should look something like:

with open("filename", "wb") as fout:
    fout.write(struct.pack(format, data, ...))

If you explain what data exactly you want to dump, then I can elaborate on the solution

If your data is just a hex string, then you do not need struct, you just use decode. Please refer to SO question hexadecimal string to byte array in python

example for python 2.7:

hex_str =  "414243444500ff"
bytestring = hex_str.decode("hex")
with open("filename", "wb") as fout:
    fout.write(bytestring)
gelonida
  • 5,327
  • 2
  • 23
  • 41
  • data is 48 bytes HEX. I am not sure what format should be used here. – Ravs Oct 31 '19 at 00:56
  • where does the data come from. can you give an example python variable, that you want to write out as bytes. Is it just a hex string with 96 hex digits? – gelonida Oct 31 '19 at 00:57
  • I added an example for simple a simple hex string. For this one you don't need struct. If you have other data, then pls be more specific and we can adapt the solution – gelonida Oct 31 '19 at 01:06
  • No, it's not hex string. It's raw 48 bytes of a binary file that represent 96 hex digits. Example: 11111111 00000000 11111111 00000000 will be - ff 00 ff 00 – Ravs Oct 31 '19 at 01:16
  • Please make sure you have a clear distinction in mind between the *data itself*, and various *representations of* the data. – Karl Knechtel Oct 31 '19 at 01:17
  • Just to be clear 48 bytes are 96 hex digits or 384 bits it's just different representations The real question is how do you want to describe these bytes in your python file? If you describe them as a string that contains 96 hex digits, then the 2nd part of my answer shows you how to convert the hex string into a byte string and how to write that byte string to the file. (example is hex string length 14 representing 7 bytes( So the main, most important question is how do you want to represent the bytes, that you want to write to disk in your python script or where do they come from. – gelonida Oct 31 '19 at 02:25
0

The below worked for me:

reserved = "Reserved_48_Bytes"
f.write(struct.pack("48s", reserved))

Output:

hexdump -C output.bin

00000030  52 65 73 65 72 76 65 64  5f 34 38 5f 42 79 74 65  |Reserved_48_Byte|    
00000040  73 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |s...............|    
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
Ravs
  • 261
  • 2
  • 7
  • 15