result = bytes.fromhex(some_hex_string)
Answer from vili on Stack Overflowresult = bytes.fromhex(some_hex_string)
Works in Python 2.7 and higher including python3:
result = bytearray.fromhex('deadbeef')
Note: There seems to be a bug with the bytearray.fromhex() function in Python 2.6. The python.org documentation states that the function accepts a string as an argument, but when applied, the following error is thrown:
>>> bytearray.fromhex('B9 01EF')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: fromhex() argument 1 must be unicode, not str`
What's the correct way to convert bytes to a hex string in Python 3? - Stack Overflow
arrays - How to convert hexadecimal string to bytes in Python? - Stack Overflow
Need some help with converting hex to bytearray for long strings
How to convert a large string having Hex digits into bytes?
Since Python 3.5 this is finally no longer awkward:
>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'
and reverse:
>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'
works also with the mutable bytearray type.
Reference: https://docs.python.org/3/library/stdtypes.html#bytes.hex
Use the binascii module:
>>> import binascii
>>> binascii.hexlify('foo'.encode('utf8'))
b'666f6f'
>>> binascii.unhexlify(_).decode('utf8')
'foo'
See this answer: Python 3 string to hex
Suppose your hex string is something like
>>> hex_string = "deadbeef"
Convert it to a bytearray (Python 3 and 2.7):
>>> bytearray.fromhex(hex_string)
bytearray(b'\xde\xad\xbe\xef')
Convert it to a bytes object (Python 3):
>>> bytes.fromhex(hex_string)
b'\xde\xad\xbe\xef'
Note that bytes is an immutable version of bytearray.
Convert it to a string (Python ≤ 2.7):
>>> hex_data = hex_string.decode("hex")
>>> hex_data
"\xde\xad\xbe\xef"
There is a built-in function in bytearray that does what you intend.
bytearray.fromhex("de ad be ef 00")
It returns a bytearray and it reads hex strings with or without space separator.
Hi all,
I'm working with the API for The Things Network and I have a number of fields that expect HEX as their input. These are then matched to the equivalent values sent by the LoRaWAN devices for "shared key" authentication.
As an example, there is a field called "net_key" which is a 16 byte string, and at the moment I'm just generating a string via secrets.hex(16), however on the end devices I need to convert that into an array with each of the couplets split out, which then needs to be converted into a byte array
For example, if the hex call returns aabbccddeeff00112233445566778899, then the byte array should look like this:
['0xaa','0xbb','0xcc','0xdd','0xee','0xff','0x00','0x11','0x22','0x33', '0x44', '0x55','0x66','0x77','0x88','0x99']
Here's an example JSON payload for the API:
{
"end_device": {
"ids": {
"device_id": "test-device"
},
"session": {
"keys": {
"app_s_key": {
"key": "32079314899832864415563434004158"
}
},
"dev_addr": "3d72ba69"
}
},
"field_mask": {
"paths": [
"ids.device_id",
"session.keys.app_s_key.key",
"session.dev_addr"
]
}
}and here's what the device configuration should look like:
ttn_config = {
"devaddr": bytearray([0x3d, 0x72, 0xba, 0x69]),
"nwkey": bytearray([]),
"app": bytearray([0x32, 0x07, 0x93, 0x14, 0x89, 0x98, 0x32, 0x86, 0x44, 0x15, 0x56, 0x34, 0x34, 0x00, 0x41, 0x58])
"country": "EU",
}Is there a an easy way that I'm missing to convert between the two formats?
What I'm trying to do is convert a string like "abcd" into bytes that represent the hexadecimal of the string oxabcd. Python keeps telling me OverflowError: int too big to convert
The code is the following:
a = "abcd" print(int(a, 16).to_bytes())
The error in full is:
Traceback (most recent call last):
File "/home/repl109/main.py", line 6, in <module>
print(int(a, 16).to_bytes())
^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convertAnyone know a way to do this for large integers?