[Fact]
public void Test()
{
long value = 1118135461923340288;
string base62 = Base62Encode(value);
TestOutputHelper.WriteLine("62进制表示: " + base62); // 1Kb4TpuVoO0
byte[] bytes = BitConverter.GetBytes(value);
string hex = BitConverter.ToString(bytes).Replace("-", "");
TestOutputHelper.WriteLine("字节数据Hex表示: " + hex);// 40B54A476A840F
string base64 = Convert.ToBase64String(bytes);
TestOutputHelper.WriteLine("Base64: " + base64);// AEC1SkdqhA8=
}
private static readonly char[] Base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
private static string Base62Encode(long value)
{
if (value == 0)
return "0";
string result = string.Empty;
while (value > 0)
{
int remainder = (int)(value % 62);
result = Base62Chars[remainder] + result;
value /= 62;
}
return result;
}
全部评论