In the .NET, the number was effectively converted into any advancement
Convert.ToString
Custom conversion method
This method uses the value to be converted and a character array, which represents the characters in the target inlet. It repeats the calculation value except for the remaining number of the target and adds the corresponding character to the result string.
<code class="language-csharp">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; }</code>
Use examples
To use this method, you can pass the value of the value of the value and the target, for example:Performance Precautions
<code class="language-csharp">// 转换为二进制 string binary = IntToString(5, new char[] { '0', '1' }); // 转换为十六进制 string hex = IntToString(42, new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}); // 转换为六十进制 string sexagesimal = 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'});</code>
The above is the detailed content of How to Efficiently Convert Numbers to Arbitrary Bases in .NET?. For more information, please follow other related articles on the PHP Chinese website!