Skip to content
Advertisement

Data type somehow gets converted when appended to a list

I am trying to convert text to binary using ASCII encoding, and then decode the binary back to text as proof of concept for a larger project. The following code works well and does what it is supposed to do:

bytes = []

input_data = "Hello"

for b in input_data:
    new_byte = bin(int.from_bytes(b.encode('ascii'), 'big'))
    bytes.append(new_byte)

for x in bytes:
    print(x)

for byte in bytes:
    byte_int = int(byte, 2)
    output_data = str(byte_int.to_bytes(byte_int, 'big').decode('ascii'))
    print(output_data)

However, as soon as I try to append the given value of output_data to a list within the for loop, the strings become converted to HEX for some odd reason.

bytes = []
output_list = []

input_data = "Hello"

for b in input_data:
    new_byte = bin(int.from_bytes(b.encode('ascii'), 'big'))
    bytes.append(new_byte)

for x in bytes:
    print(x)

for byte in bytes:
    byte_int = int(byte, 2)
    output_data = str(byte_int.to_bytes(byte_int, 'big').decode('ascii'))
    print(output_data)
    output_list.append(output_data)

print(output_list)

With the output:

0b1001000
0b1100101
0b1101100
0b1101100
0b1101111
H
e
l
l
o
['x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00H', 'x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00e', 'x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00l', 'x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00l', 'x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00o']

I have no idea why this happens, is there any explanation?

Advertisement

Answer

You misused the int.to_bytes method. As the doc states,

int.to_bytes(length, byteorder, *, signed=False)

Return an array of bytes representing an integer. […]

The integer is represented using length bytes.

You used byte_int.to_bytes(byte_int, 'big'), so you convert for example the integer 72 into a byte string of length 72, which gives you 71 null bytes before the one representing your value.

You want a single byte, so use

output_data = str(byte_int.to_bytes(1, 'big').decode('ascii'))
Advertisement