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:

JavaScript

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.

JavaScript

With the output:

JavaScript

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

JavaScript
Advertisement