Encode (& decode) a URL as a string of letters
I want a function that can encode any URL into a string of all letters (upper and lowercase), and another function to decode it back into a URL. What's the best way to accomplish this?
Sample API:
&g开发者_运维技巧t; 'http://stackoverflow.com/questions/ask'.url_to_chars
=> 'mgzGBORuRcFSfNXDpDbVgzzvANHLqIEcgjCAXsKbNXGouOckToKkZRBnvE'
> 'mgzGBORuRcFSfNXDpDbVgzzvANHLqIEcgjCAXsKbNXGouOckToKkZRBnvE'.chars_to_url
=>'http://stackoverflow.com/questions/ask'
Base64 is a simple way to do this:
String encoded = Base64.encode("http://stackoverflow.com/questions/ask".getBytes());
System.out.println(encoded);
System.out.println(new String(Base64.decode(encoded)));
Prints:
aHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucy9hc2s=
http://stackoverflow.com/questions/ask
Update:
If you actually look at the RFC 1738 URLs are case-insensitve and only a range of characters are allowed. There's plenty of space to map it as long as your input strings are valid encoded URLs.
import string
l = string.ascii_letters + string.digits
t = string.ascii_lowercase + string.digits + ";/?:@=&$-_.+!*'(),"
d = dict(zip(l,t))
e = dict(zip(t,l))
d and e are the decoding and the reverse encoding mapping.
[('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e'), ('f', 'f'), ('g', 'g'), ('h', 'h'), ('i', 'i'), ('j', 'j'), ('k', 'k'), ('l', 'l'), ('m', 'm'), ('n', 'n'), ('o', 'o'), ('p', 'p'), ('q', 'q'), ('r', 'r'), ('s', 's'), ('t', 't'), ('u', 'u'), ('v', 'v'), ('w', 'w'), ('x', 'x'), ('y', 'y'), ('z', 'z'), ('0', 'A'), ('1', 'B'), ('2', 'C'), ('3', 'D'), ('4', 'E'), ('5', 'F'), ('6', 'G'), ('7', 'H'), ('8', 'I'), ('9', 'J'), (';', 'K'), ('/', 'L'), ('?', 'M'), (':', 'N'), ('@', 'O'), ('=', 'P'), ('&', 'Q'), ('$', 'R'), ('-', 'S'), ('_', 'T'), ('.', 'U'), ('+', 'V'), ('!', 'W'), ('*', 'X'), ("'", 'Y'), ('(', 'Z'), (')', '0'), (',', '1')]
Decode and encode are only simple mappings:
def encode(s): return ''.join(e[c] for c in s)
def decode(s): return ''.join(d[c] for c in s)
The output is:
enc = encode("http://stackoverflow.com/questions/ask")
>>> decode(enc)
'http://stackoverflow.com/questions/ask'
>>> enc
'httpNLLstackoverflowUcomLquestionsLask'
You can make use of base64 encoding and decoding.
Depending on the data you can use some encryption and decryption algorithm to accomplish this. It will put the string into text with no special characters etc. With the added bonus that the data is encrypted.
精彩评论