I am a very beginner at Python. I have a string that is “TEST00000001” and its length is 12. I want to convert this string to a byte with a hexadecimal representation like this b’x54x45x53x54x30x30x30x30x3030x30x31′. As you can see, the length of the two objects has not changed.
So far, I am successfully converting my string to bytes. But the length of converted bytes is 24 instead of 12.
Here is what I’ve done so far:
sample_string = "TEST00000001" output = ''.join(hex(ord(character_index))[2:] for character_index in sample_string) hex_bytes = bytes(output, 'ascii') print(hex_bytes) # Output of this line is: b'544553543030303030303031' print(len(hex_bytes)) # Length of my output is: 24
Can someone help me about converting my string to a byte object (in hex representation) without changing its length?
Advertisement
Answer
To convert a string into bytes in Python, use bytes
directly on the string. Don’t call ord
or hex
on the characters:
hex_string2 = "TEST00000001" byte_data = bytes(hex_string2, 'ascii') print(len(byte_data)) # 12
And the output is indeed the same as b'x54x45x53x54x30x30x30x30x30x30x30x31'
:
byte_data == b'x54x45x53x54x30x30x30x30x30x30x30x31' # True
By contrast, if your input is a string of bytes in padded hexadecimal representation, you need to decode that representation (e.g. using binascii
):
import binascii binascii.unhexlify(b'544553543030303030303031') # b'TEST00000001'
Or codecs
:
import codecs codecs.decode('544553543030303030303031', 'hex') # b'TEST00000001'