如何将 int64 快速转换(重新编码)为字符限制字符串?
How to fast convert (recode) int64 to character-limited String?
我正在寻找一种方法来将仅由数字 [0,1,2,...8,9] 组成的字符串重新编码为另一个具有不同字符集的字符串,例如数字和字母 [0,1 ,...9,a,b,...z](作为示例)。结果字符串通常应该比纯数字字符串短。该方法应该是可逆的并且应该是快速的。 C# 答案中的源代码将不胜感激,但也欢迎提出一般性想法;-)
示例:
Input: "1234567890123"
Output: "ar4cju7d"
应用于"ar4cju7d"的反转方法的输出应该是“1234567890123.
如果您的输入始终是 Int64,那么您可以检查 Base36
encoding. And here's a sample implementation
:
// Edit: Slightly updated on 2011-03-29
/// <summary>
/// A Base36 De- and Encoder
/// </summary>
public static class Base36
{
private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
/// <summary>
/// Encode the given number into a Base36 string
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static String Encode(long input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray());
}
/// <summary>
/// Decode the Base36 Encoded string into a number
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Int64 Decode(string input)
{
var reversed = input.ToLower().Reverse();
long result = 0;
int pos = 0;
foreach (char c in reversed)
{
result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
pos++;
}
return result;
}
}
可以应用于您的示例:
long input = 1234567890123;
string encoded = Base36.Encode(input); // yields "fr5hugnf"
long originalInput = Base36.Decode(encoded); // yields 1234567890123
我正在寻找一种方法来将仅由数字 [0,1,2,...8,9] 组成的字符串重新编码为另一个具有不同字符集的字符串,例如数字和字母 [0,1 ,...9,a,b,...z](作为示例)。结果字符串通常应该比纯数字字符串短。该方法应该是可逆的并且应该是快速的。 C# 答案中的源代码将不胜感激,但也欢迎提出一般性想法;-) 示例:
Input: "1234567890123"
Output: "ar4cju7d"
应用于"ar4cju7d"的反转方法的输出应该是“1234567890123.
如果您的输入始终是 Int64,那么您可以检查 Base36
encoding. And here's a sample implementation
:
// Edit: Slightly updated on 2011-03-29
/// <summary>
/// A Base36 De- and Encoder
/// </summary>
public static class Base36
{
private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
/// <summary>
/// Encode the given number into a Base36 string
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static String Encode(long input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray());
}
/// <summary>
/// Decode the Base36 Encoded string into a number
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Int64 Decode(string input)
{
var reversed = input.ToLower().Reverse();
long result = 0;
int pos = 0;
foreach (char c in reversed)
{
result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
pos++;
}
return result;
}
}
可以应用于您的示例:
long input = 1234567890123;
string encoded = Base36.Encode(input); // yields "fr5hugnf"
long originalInput = Base36.Decode(encoded); // yields 1234567890123