Home > Backend Development > C++ > How to Efficiently Convert Numbers to Arbitrary Bases in .NET?

How to Efficiently Convert Numbers to Arbitrary Bases in .NET?

Barbara Streisand
Release: 2025-01-28 10:16:12
Original
450 people have browsed it

How to Efficiently Convert Numbers to Arbitrary Bases in .NET?

In the .NET, the number was effectively converted into any advancement

Converting decimal numbers into any other in -system is a common task in programming. Although the .NET provides a method to convert to a limited transmission set, developers usually need a more flexible method, which supports any advancement and any character set.

Convert.ToString Custom conversion method

In order to convert numbers into any advancement, you can implement a custom method, which iterates the number in the target progress. This is a simplified version of this 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>
Copy after login

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>
Copy after login
The above method is connected to construct the result string with a string. For large numbers, this may be slow. The improved version will use array buffer to accumulate characters.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template