问题描述
我有一个我写的旧的(ish)C#方法,它接受一个数字并将其转换为任何基数:
I have and old(ish) C# method I wrote that takes a number and converts it to any base:
string ConvertToBase(int number, char[] baseChars);
这并不是超级快速和整洁.在 .NET 中是否有一种已知的好方法可以实现这一目标?
It's not all that super speedy and neat. Is there a good, known way of achieving this in .NET?
我正在寻找允许我使用 any 基数和任意字符串的东西.
I'm looking for something that allows me to use any base with an arbitrary string of characters to use.
这仅允许碱基 16、10、8 和 2:
This only allows bases 16, 10, 8 and 2:
Convert.ToString(1, x);
我想利用数字、所有小写字母和所有大写字母来使用它来实现非常高的基数.就像在 这个线程中一样,但对于 C# 而不是 JavaScript.
I want to use this to achieve a massively high base taking advantage of numbers, all lower case and all upper case letters. Like in this thread, but for C# not JavaScript.
有人知道在 C# 中执行此操作的好方法吗?
Does anyone know of a good and efficient way of doing this in C#?
推荐答案
Convert.ToString
可用于将数字转换为其在指定基数中的等效字符串表示形式.
Convert.ToString
can be used to convert a number to its equivalent string representation in a specified base.
例子:
string binary = Convert.ToString(5, 2); // convert 5 to its binary representation
Console.WriteLine(binary); // prints 101
但是,正如评论所指出的,Convert.ToString
仅支持以下有限但通常足够的基数集:2、8、10 或 16.
However, as pointed out by the comments, Convert.ToString
only supports the following limited - but typically sufficient - set of bases: 2, 8, 10, or 16.
我不知道 BCL 中有任何方法能够将数字转换为任何基数,因此您必须编写自己的小型实用程序函数.一个简单的示例看起来像这样(请注意,这肯定可以通过替换字符串连接来更快):
I'm not aware of any method in the BCL which is capable to convert numbers to any base so you would have to write your own small utility function. A simple sample would look like that (note that this surely can be made faster by replacing the string concatenation):
class Program
{
static void Main(string[] args)
{
// convert to binary
string binary = IntToString(42, new char[] { '0', '1' });
// convert to hexadecimal
string hex = IntToString(42,
new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'});
// convert to hexavigesimal (base 26, A-Z)
string hexavigesimal = IntToString(42,
Enumerable.Range('A', 26).Select(x => (char)x).ToArray());
// convert to sexagesimal
string xx = IntToString(42,
new char[] { '0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x'});
}
public static string IntToString(int value, char[] baseChars)
{
string result = string.Empty;
int targetBase = baseChars.Length;
do
{
result = baseChars[value % targetBase] + result;
value = value / targetBase;
}
while (value > 0);
return result;
}
/// <summary>
/// An optimized method using an array as buffer instead of
/// string concatenation. This is faster for return values having
/// a length > 1.
/// </summary>
public static string IntToStringFast(int value, char[] baseChars)
{
// 32 is the worst cast buffer size for base 2 and int.MaxValue
int i = 32;
char[] buffer = new char[i];
int targetBase= baseChars.Length;
do
{
buffer[--i] = baseChars[value % targetBase];
value = value / targetBase;
}
while (value > 0);
char[] result = new char[32 - i];
Array.Copy(buffer, i, result, 0, 32 - i);
return new string(result);
}
}
更新 2(性能改进)
使用数组缓冲区而不是字符串连接来构建结果字符串可以提高性能,尤其是在大数字上(参见方法 IntToStringFast
).在最好的情况下(即可能的最长输入),这种方法大约快三倍.但是,对于 1 位数字(即目标基数中的 1 位数字),IntToString
会更快.
Update 2 (Performance Improvement)
Using an array buffer instead of string concatenation to build the result string gives a performance improvement especially on large number (see method IntToStringFast
). In the best case (i.e. the longest possible input) this method is roughly three times faster. However, for 1-digit numbers (i.e. 1-digit in the target base), IntToString
will be faster.
这篇关于将基数为 10 的数字转换为 .NET 中任何基数的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!