I am new to python and was trying to do a simple job but I am having a problem. I am trying to take a large number as input from user say 500 and write it to a file in hex format. For this I tried to take a small number as first step and write it, but I get the error as below
10
0xa
Traceback (most recent call last):
File "temp.py", line 7, in <module>
fp.write(bytes(('num',))) TypeError: 'str' object cannot be interpreted as an integer
The sample code used is as below.
intval = int(input())
num = hex(intval)
print(num)
with open('hexFile.iotabin', 'wb') as fp:
fp.write(bytes(('num',)))
fp.close()
Could you let me know
- What am I doing wrong here?
- For large number say 500, I would like to split it to two bytes while writing. Will to_bytes() be able to help once the issue above is resolved or is there any other simple way.
source https://stackoverflow.com/questions/69360021/writing-the-user-input-to-a-binary-file-in-hex-format-using-python-3-8
Comments
Post a Comment