I would create a python script that decode a Base64 string to an array of byte (or array of Hex values).
The embedded side of my project is a micro controller that creates a base64 string starting from raw byte. The string contains some no-printable characters (for this reason I choose base64 encoding).
On the Pc side I need to decode the the base64 string and recover the original raw bytes.
My script uses python 2.7 and the base64 library:
base64Packet = raw_input('Base64 stream:')
packet = base64.b64decode(base64Packet )
sys.stdout.write("Decoded packet: %s"%packet)
The resulting string is a characters string that contains some not printable char.
Is there a way to decode base64 string to byte (or hex) values?
Thanks in advance!
%r
instead when printing? – Martijn Pieters♦str
object is already a sequence, you can address each byte withpacket[index]
, for example, or loop over the string withfor byte in packet:
. – Martijn Pieters♦list(packet)
, producing a list of 1-character (byte) strings. Or perhaps you want to use abytearray
object instead, but it isn't clear what you want to do with your data. You haven't given us your use case. – Martijn Pieters♦bytearray()
instance; just usebytearray(packet)
. A bytearray is a mutable sequence of integers in the range 0-255, one integer per byte. – Martijn Pieters♦