How to convert a string to bytes with hexadecimal representation in Python?

Question:

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?

Asked By: fenderogi

||

Answers:

A byte is a byte, no matter the architecture.
As far as I have been unable to reproduce your problems, I’ve seen this:

>>> hex_string2 = "TEST00000001"
>>> byte_data = bytes(hex_string2, 'ascii')
>>> print(byte_data) #/ b'544553543030303030303031'
b'TEST00000001'
>>> print(len(byte_data)) #24
12

The above works, as expected.
Can it be, that in your original post, you have a different variable that you are referencing and setting before (i.e. variable output)?

byte_data = bytes(output, 'ascii')

Beacause if you do:

>>> hex_string2.encode("hex")
'544553543030303030303031'

Now if you would want a list of two digit decimals, then

>>> [this.encode("hex") for this in byte_data]
['54', '45', '53', '54', '30', '30', '30', '30', '30', '30', '30', '31']

Would this be something you are trying to get?

Answered By: Ventil

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'
Answered By: Konrad Rudolph
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.