base_convert在.NETbase_convert、NET

2023-09-03 07:48:32 作者:续写不尽的爱恋

.NET是否有一个本机的功能,等效于PHP的 base_convert 或者我需要写我自己?我想从任何基本转换为任何其他基地 - 在这里无论是'为'基地或从'基地可以是任意整数2-36

Does .NET have a native function that is equivalent to PHP's base_convert or will I need to write my own? I want to convert from any base to any other base -- where either the 'to' base or the 'from' base can be any integer 2-36.

的PHP函数实例:base_convert($ number_to_convert,$ from_base,$ to_base)

Example of the PHP function: base_convert($number_to_convert, $from_base, $to_base)

// convert 101 from binary to decimal
echo base_convert('101', 2, 10);
// 5

正如在乔恩斯基特的回答的注释注意到卢克:Convert.ToString不能处理转换到/从任意的基础上,只有2,8,10和16

As noted by Luke in the comments of Jon Skeet's answer: Convert.ToString can't handle conversion to/from any arbitrary base, only 2, 8, 10 and 16

更新:显然,答案是:不,没有提供原生的方式。下面,埃里克表明要做到这一点的方法之一。另一种实现是在这里: HTTP://www.$c$cproject.com/ KB /宏/ Convert.aspx

Update: Apparently, the answer is: no, there is no native way. Below, Erik shows one way to do this. Another implementation is here: http://www.codeproject.com/KB/macros/Convert.aspx

推荐答案

下面是一些code,它会转换成一个整数到任意基地达36,并转换为字符串重新基地的x presentation值的整数(给定的基):

Here's some code that'll convert an integer to an arbitrary base up to 36, and convert a string representation of a base x value to an integer (given the base):

class Program {
	static void Main(string[] args) {
		int b10 = 123;
		int targetBase = 5;

		string converted = ConvertToBase(b10, targetBase);
		int convertedBack = ConvertFromBase(converted, targetBase);

		string base3 = "212210";
		string base7 = ConvertFromBaseToBase(base3, 3, 7);

		Console.WriteLine(converted);
		Console.WriteLine(convertedBack);
		Console.WriteLine(base7);
		Console.ReadLine();
	}

	private const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	private static string ConvertToBase(int b10, int targetBase) {
		if (targetBase < 2) throw new ArgumentException("Target base must be greater than 2.", "targetBase");
		if (targetBase > 36) throw new ArgumentException("Target base must be less than 36.", "targetBase");

		if (targetBase == 10) return b10.ToString();

		StringBuilder result = new StringBuilder();

		while (b10 >= targetBase) {
			int mod = b10 % targetBase;
			result.Append(chars[mod]);
			b10 = b10 / targetBase;
		}

		result.Append(chars[b10]);

		return Reverse(result.ToString());
	}

	private static int ConvertFromBase(string bx, int fromBase) {
		if (fromBase < 2) throw new ArgumentException("Base must be greater than 2.", "fromBase");
		if (fromBase > 36) throw new ArgumentException("Base must be less than 36.", "fromBase");

		if (fromBase == 10) return int.Parse(bx);

		bx = Reverse(bx);
		int acc = 0;

		for (int i = 0; i < bx.Length; i++) {
			int charValue = chars.IndexOf(bx[i]);
			acc += (int)Math.Pow(fromBase, i) * charValue;
		}

		return acc;
	}

	public static string ConvertFromBaseToBase(string bx, int fromBase, int toBase) {
		int b10 = ConvertFromBase(bx, fromBase);
		return ConvertToBase(b10, toBase);
	}

	public static string Reverse(string s) {
		char[] charArray = new char[s.Length];
		int len = s.Length - 1;
		for (int i = 0; i <= len; i++)
			charArray[i] = s[len - i];
		return new string(charArray);
	}
}

如果你不关心显示这些值,可以使用扩展字符在字符集 - 如果你坚持纯ASCII,理论上可以有base256值。超越,我会建议不要使用字符,但使用而不是一些其他的唯一可识别的价值 - 虽然我没有看到太多的价值

If you're unconcerned with displaying these values, you can use extended characters in your chars set - if you stick to plain ascii, you can theoretically have base256 values. Going beyond that I would recommend not using chars, but instead using some other uniquely-identifiable value - though I don't much see the value.

 
精彩推荐
图片推荐