110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static string Base62Encode(long num)
{
string s = "";
char[] metachar = Enumerable.Range(0, 10).Select(x => (char)('0' + x))
.Concat(Enumerable.Range(0, 26).Select(x => (char)('a' + x)))
.Concat(Enumerable.Range(0, 26).Select(x => (char)('A' + x)))
.ToArray();
while (num != 0)
{
s = metachar[num % 62].ToString() + s;
num = num / 62;
}
return s;
}
static long Base62Decode(string s)
{
var dict = Enumerable.Range(0, 10).Select(x => (char)('0' + x))
.Concat(Enumerable.Range(0, 26).Select(x => (char)('a' + x)))
.Concat(Enumerable.Range(0, 26).Select(x => (char)('A' + x)))
.Select((x, i) => new { x, i }).ToDictionary(x => x.x, x => x.i);
long l = 0;
for (int i = 0; i < s.Length; i++)
{
l = l + (long)dict[s[i]] * (long)Math.Pow((double)62, (double)(s.Length - i - 1));
}
return l;
}
static void Main(string[] args)
{
long l = 999999999999;
string s = Base62Encode(l);
Console.WriteLine(s);
Console.WriteLine(Base62Decode(s));
}
}
}
hBxM5A3
999999999999
Press any key to continue . . . public string Ten2Hex(string ten)
{
ulong tenValue = Convert.ToUInt64(ten);
ulong divValue, resValue;
string hex = "";
do
{
divValue = (ulong)Math.Floor(Convert.ToDouble(tenValue / 62));
resValue = tenValue % 62;
hex = tenValue2Char(resValue) + hex;
tenValue = divValue;
}
while (tenValue >= 62);
if (tenValue != 0)
hex = tenValue2Char(tenValue) + hex;
return hex;
}
public string tenValue2Char(ulong ten)
{
switch (ten)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return ten.ToString();
case 10: return "a";
case 11: return "b";
case 12: return "c";
case 13: return "d";
case 14: return "e";
case 15: return "f";
case 16: return "g";
case 17: return "h";
case 18: return "i";
case 19: return "j";
case 20: return "k";
case 21: return "l";
case 22: return "m";
case 23: return "n";
case 24: return "o";
case 25: return "p";
case 26: return "q";
case 27: return "r";
case 28: return "s";
case 29: return "t";
case 30: return "u";
case 31: return "v";
case 32: return "w";
case 33: return "s";
case 34: return "y";
case 35: return "z";
case 36: return "A";
case 37: return "B";
case 38: return "C";
case 39: return "D";
case 40: return "E";
case 41: return "F";
case 42: return "G";
case 43: return "H";
case 44: return "I";
case 45: return "J";
case 46: return "K";
case 47: return "L";
case 48: return "M";
case 49: return "N";
case 50: return "O";
case 51: return "P";
case 52: return "Q";
case 53: return "R";
case 54: return "S";
case 55: return "T";
case 56: return "U";
case 57: return "V";
case 58: return "W";
case 59: return "S";
case 60: return "Y";
case 61: return "Z";
default: return "";
}
}
MessageBox.Show(Ten2Hex("999999999999"));
结果:hBsM5A3static string L2S(long l)
{
byte[] bytes = BitConverter.GetBytes(l).Reverse().SkipWhile(b => b == 0).ToArray();
return Convert.ToBase64String(bytes).Replace("=","");
}
static long S2L(string s)
{
s = s.PadRight((s.Length + 3) / 4 * 4, '=');
byte[] bytes = Convert.FromBase64String(s).Reverse().Concat(new byte[8]).Take(8).ToArray();
return BitConverter.ToInt64(bytes, 0);
}
static void Main(string[] args)
{
string s1 = L2S(999999999999); // 6NSlD/8
long l1 = S2L(s1); // 999999999999
string s2 = L2S(123); // ew
long l2 = S2L(s2); // 123
}