Base62 encoding/decoding


#1

Hi,
can anyone help me in base62 encoding??

if i Encode “ilovemyworld” i should get a code with format[A-Za-z0-9].
If i decode the format, i should get back “ilovemyworld”

For Example,
encode “ilovemyworld” # eDr4e3S
decode “eDr4e3S” # ilovemyworld

Thanks,
Srikanth.J


#2

On Tue, May 5, 2009 at 1:10 PM, Srikanth J. removed_email_address@domain.invalid
wrote:

Hi,
can anyone help me in base62 encoding??

if i Encode “ilovemyworld” i should get a code with format[A-Za-z0-9].
If i decode the format, i should get back “ilovemyworld”

For Example,
encode “ilovemyworld” # eDr4e3S
decode “eDr4e3S” # ilovemyworld

Would make for a good ruby quiz.

I don’t think it’s possible, though, unless you change your
intermittent base. 64 is there because “111111”.to_i(2) is 63. In
other words the information you store by bits (0 and 1) will have to
cover the gamut of 64 characters and no less. Now, you can limit the
number of characters to 32 (using 5 bits), if you’re willing to have
the encoding severely larger.

If you used something other than “bits”, like a different base, I
thought it would be possible, but 62 is only divisible by 2 and 31.

I suppose the pragmatic approach would be to take your undesirable
characters and alter them; replacing “=” with “555”, for example.

I never actually knew anything about base64, so thanks for getting me
to learn something :slight_smile:

Todd


#3

On Wed, May 6, 2009 at 2:26 AM, Todd B. removed_email_address@domain.invalid wrote:

I don’t think it’s possible, though, unless you change your
intermittent base. 64 is there because “111111”.to_i(2) is 63. In

it’s definitely possible, just inefficient. first you set up your lookup
table:

digits = (0…9).to_a.map(&:to_s) + (“a”…“z”).to_a + (“A”…“Z”).to_a

then you repeatedly find the lowest digit

out = “”
while n > 0
rest, units = n.divmod(62)
out = digits[units] + out
n = rest
end

martin


#4

On Wed, May 6, 2009 at 11:32 AM, Martin DeMello
removed_email_address@domain.invalid wrote:

rest, units = n.divmod(62)
out = digits[units] + out
n = rest
end

martin

Isn’t that just a simple cipher (i.e. map)? I must be missing
something. According to what I’ve read so far, base64 is not, and
base62 is, except for that paper written in a scientific journal that
I don’t have access to (but, for the summary, of course). I suppose
that is what the OP wanted anyway.

Todd


#5

On Wed, May 6, 2009 at 11:42 PM, Todd B. removed_email_address@domain.invalid
wrote:

something. According to what I’ve read so far, base64 is not, and
base62 is, except for that paper written in a scientific journal that
I don’t have access to (but, for the summary, of course). I suppose
that is what the OP wanted anyway.

No, it’s a number base transformation. Here’s an example using base 7
(as being easier to work with than 62 :)):

letting n = 1250, and using # as a divmod operator:

1250 # 7 = 178, 4
178 # 7 = 25, 3
25 # 7 = 3, 4
3 # 7 = 0, 3 <-- we have reached n=0, so the loop terminates

so 1250[base 10] = 3434 [base 7]

If you think about it, base 10 works the same way:

1250 # 10 = 125, 0
125 # 10 = 12, 5
12 # 10 = 1, 2
1 # 10 = 0, 1

so 1250[base 10] = 1250[base 10]

To go the other way, you repeatedly add the least significant digit
and multiply by the base

so 3434[7]

start with 0, and read the digits in forward order
(0 * 7) + 3 = 3
3 * 7 + 4 = 25
24 * 7 + 3 = 178
178 * 7 + 4 = 1250 <— et voila!

martin

martin