Search code examples
pythonbase64

Given a string, how do I know if it needs decoding


I'm using python's base64 module and I get a string that can be encoded or not encoded. I would like to do something like:

if isEncoded(s):
   output = base64.decodestring(s)
else:
   output = s

ideas?


Solution

  • In general, it's impossible; if you receive string 'MjMj', for example, how could you possibly know whether it's already decoded and needs to be used as is, or decoded into '23#'?