开发者

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?


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#'?


You could just try it, and see what happens:

import base64

def decode_if_necessary(s):
    try:
         return base64.decodestring(s)
    except:
         return s

But you have to ask yourself: what if the original message was in fact a syntactically valid base64 string, but not meant to be one? Then "decoding" it will succeed, but the result is not the required output. So I have to ask: is this really what you want?

Edit: Note that decodestring is deprecated.


You could check to see if a string may be base64 encoded. In general, the function can predict with 75%+ accuracy is the data is encoded.

def isBase64(s):
    return (len(s) % 4 == 0) and re.match('^[A-Za-z0-9+/]+[=]{0,2}$', s)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜