Convert a string into BASE62

后端 未结 3 1498
情书的邮戳
情书的邮戳 2021-02-10 13:15

I\'m looking for the c# code to convert a string into BASE62, like this:

http://www.molengo.com/base62/title/base62-encoder-decoder

I need those encode and decod

3条回答
  •  花落未央
    2021-02-10 13:37

    You can do this for any base, this way:

    static string ToBase62(ulong number)
    {
    var alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var n = number;
    ulong basis = 62;
    var ret = "";
    while (n > 0)
     {
       ulong temp = n % basis;
       ret = alphabet[(int)temp] + ret;
       n = (n / basis);
    
     }
     return ret;
    }
    

提交回复
热议问题