Tag: base64

Base X Encoding

Ever needed to shorten a number so that its easier to remember? Or provide someone with a temporary PIN which is short enough to remember, but long enough to pretty much ensure it wont be randomly guessed by someone else? Converting a binary number into a hexadecimal is exactly the process used in such cases. But hexadecimal only has 16 characters in its "dictionary". Base64 is the next step up, with a bigger dictionary containing all alphanumerics (upper and lower case) as well as "/" and "+". I need a solution which didn't contain certain characters. For example, its easy to mix up an O with a 0. Or an I,l and a 1. I wanted a solution whereby I could encode a number, but using my own definition of the dictionary. So I built just such a solution. You can see the source code below. It contains a main method which runs a simple test, the output of which is:     Original: 123456789012345678901234567890     encoded: 2aYls9bkamJJSwhr0     decoded: 123456789012345678901234567890     Passed! decoded value is the same as the original. As you can see, the encoded version is only half as long as the input. Using an 89 character dictionary, it gets even shorter:     encoded: "9Kgbz])M.w8KgK The implementation uses the BigInteger class from Java, so you can encode REALLY big numbers. My phone number is now only 5 characters long and really easy to remember:     rDm3T /* * Copyright (c) 2010 Ant Kutschera, maxant * * The code…

Read more