Base62 encoding/decoding

Discussion in 'Ruby' started by Srikanth Jeeva, May 5, 2009.

  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

    http://www.srikanthjeeva.blogspot.com
    --
    Posted via http://www.ruby-forum.com/.
    Srikanth Jeeva, May 5, 2009
    #1
    1. Advertising

  2. On Wed, May 6, 2009 at 2:26 AM, Todd Benson <> wrote:
    >
    > I don't think it's possible, though, unless you change your
    > intermittent base. =A064 is there because "111111".to_i(2) is 63. =A0In


    it's definitely possible, just inefficient. first you set up your lookup ta=
    ble:

    > digits =3D (0..9).to_a.map(&:to_s) + ("a".."z").to_a + ("A".."Z").to_a
    >


    then you repeatedly find the lowest digit

    > out =3D ""
    > while n > 0
    > rest, units =3D n.divmod(62)
    > out =3D digits[units] + out
    > n =3D rest
    > end


    martin
    Martin DeMello, May 6, 2009
    #2
    1. Advertising

  3. Srikanth Jeeva

    Todd Benson Guest

    On Wed, May 6, 2009 at 11:32 AM, Martin DeMello <> wrote:
    >
    > 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
    >
    >


    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
    Todd Benson, May 6, 2009
    #3
  4. On Wed, May 6, 2009 at 11:42 PM, Todd Benson <> wrote:
    > On Wed, May 6, 2009 at 11:32 AM, Martin DeMello <>=

    wrote:
    >>
    >>> out =3D ""
    >>> while n > 0
    >>> =A0 =A0rest, units =3D n.divmod(62)
    >>> =A0 =A0out =3D digits[units] + out
    >>> =A0 =A0n =3D rest
    >>> end

    >
    > Isn't that just a simple cipher (i.e. map)? =A0I must be missing
    > something. =A0According 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). =A0I 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 =3D 1250, and using # as a divmod operator:

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

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

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

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

    so 1250[base 10] =3D 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 =3D 3
    3 * 7 + 4 =3D 25
    24 * 7 + 3 =3D 178
    178 * 7 + 4 =3D 1250 <--- et voila!

    martin


    martin
    Martin DeMello, May 7, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Slade

    Problem encoding/decoding image

    Slade, Jun 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,106
    Natty Gur
    Jun 25, 2003
  2. =?Utf-8?B?TWFyaw==?=

    query string encoding/decoding

    =?Utf-8?B?TWFyaw==?=, Mar 3, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    17,172
    T Conti
    Apr 5, 2004
  3. terry
    Replies:
    2
    Views:
    2,420
    terry
    Nov 3, 2003
  4. LarsM
    Replies:
    18
    Views:
    1,140
    Andreas Prilop
    Feb 11, 2005
  5. Intransition

    Standard Base62 Encoding?

    Intransition, Nov 1, 2010, in forum: Ruby
    Replies:
    4
    Views:
    654
    Intransition
    Nov 2, 2010
Loading...

Share This Page