Encoding is new to me in Python, and I am trying to understand it. Apologies if this has been asked and answered already.
I am trying to encode a Python list and decode it.When I am trying to encode a list directly, I am hitting an error like below.
>>> my_list = [1, 2, 3]>>> encoded_list = base64.b64encode(my_list)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 54, in b64encode encoded = binascii.b2a_base64(s)[:-1]TypeError: b2a_base64() argument 1 must be string or buffer, not list
To fix it, I converted the list object to a string and passed it to the encode function and I was able to successfully encode it.
>>> encoded_list = base64.b64encode(str(my_list))>>> encoded_list'WzEsIDIsIDNd'
When I try to decode it, I get a decoded string like below.
>>> decoded_list = base64.b64decode(encoded_list)>>> decoded_list'[1, 2, 3]'>>> type(decoded_list)<type 'str'>
But my original intention was to encode and decode a list and not convert the list to a string and then string to list.
Pretty sure this is not the right way to encode objects like dict or a list. If that's the case, Can someone please enlighten me on how to encode/decode non string objects in Python?
Thanks very much.
I am using Gmail API in order to read the emails from Gmail account. I am taking the body and I am replacing some chars which are needed as I read in the forums:
String codedBody = body.Replace("-", "+"); codedBody = codedBody.Replace("_", "/");
Problem is that when I try to convert it
byte data = Convert.FromBase64String(codedBody);
there is an exception which is firing with some emails:
System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.'
The string which is coming from the request is:
Any ideas what is causing this problem?
I Know base64 encoding is unique, but while decoding due to (padding or hex stuff) it can make two differently encoded strings to result into a same decoded string.
EDIT: one case
QzNWwQ== will decode to = C3V
QzNWwc== will also decode to = C3V
so how to make every decoding unique?
I am facing a major issue in encoding/decoding a base 64 image.
My purpose is to save a canvas image as base64 decoded string that can later be base64 encoded in php.
var string= canvas.toDataURL("image/jpeg");var abc = window.atob(string.split(","));var blob = "HEADERS"+abc;
blob is the file.
$Data = blob File$img = substr($Data, $start, $end);$img = base64_encode($img);
I start when I find chr(0xFF) . chr(0xD8) in the blob file and end when I find chr(0xFF) . chr(0xD9)