I want to do the following in Python 2.7.13:
58829405413336430
should become d101085402656e
)My code currently looks like this:
mime=58829405413336430
payload="c9999"
fw_file=open('testhex', 'wb')
fw_file.write("%x" % mime)
fw_file.write(str(payload).encode("hex"))
fw_file.close()
And I get the following file (Using xxd
on Debian):
xxd HACKEDTOGETHER
00000000: 6431 3031 3038 3534 3032 3635 3665 3633 d101085402656e63
00000010: 3339 3339 3339 3339 39393939
which is not what i need. I need a file that looks like this:
xxd WORKING
00000000: d101 0854 0265 6e63 3939 3939 ...T.enc9999
My understanding is the following:
"%x" % mime
converted my int to hex, but it was written as a String. encode
did it correctly, but that doesn't work with integers. How can I circumvent this behavior and write "pure" hex to my file? If it is not possible to do it in Python 2 i can also use Python 3.
As this is my first question on StackOverflow please tell me if I should do anything different.
Part of the problem is that the result of "%x" % mime
is a string of the pairs of hexadecimal characters — "d101085402656e"
in this case — which represents the value of the integer mime
in that format, so that's what is being written to the file. However what's needed are the actual byte values that comprise the integer itself.
In Python 3, this could be easily solved by instead using a built-in method called to_bytes()
that was added to the int
type in that version, but in Python 2.x it has to be done another way.
There's similar issue due to your use of str(payload).encode("hex")
which is also returning a hexadecimal string representation — not the actual byte values of each the characters already in payload
, which is what is needed. Fortunately in this case what needs to be can be easily accomplished by using the built-in bytearray
class Python 2.x has.
Below a version I wrote of a function posted as part of an answer to a related question that can deal with the issue with the mime
value.
Following that function definition is code showing how to use it in this case, along with the bytearray
class, to get the proper binary data written to the file.
def int_to_bytes(n, minlen=0):
""" Convert integer to bytearray with optional minimum length.
"""
if n > 0:
arr = []
while n:
n, rem = n >> 8, n & 0xff
arr.append(rem)
b = bytearray(reversed(arr))
elif n == 0:
b = bytearray(b'\x00')
else:
raise ValueError('Only non-negative values supported')
if minlen > 0 and len(b) < minlen: # zero padding needed?
b = (minlen-len(b)) * '\x00' + b
return b
mime = 58829405413336430
payload = 'c9999'
with open('testhex', 'wb') as fw_file:
fw_file.write(int_to_bytes(mime))
fw_file.write(bytearray(payload))
Here's a hex dump of contents of the testhex
file it produced (with Python 2.7.15):
00000000h: D1 01 08 54 02 65 6E 63 39 39 39 39 ; Ñ..T.enc9999
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.