Home > Backend Development > C++ > How to Efficiently Convert a Base 10 Integer to Any Base in C#?

How to Efficiently Convert a Base 10 Integer to Any Base in C#?

Mary-Kate Olsen
Release: 2025-01-28 10:33:08
Original
427 people have browsed it

How to Efficiently Convert a Base 10 Integer to Any Base in C#?

C#efficiently realize the conversion of decimal integers to any advancement

The method of C#easily converts numbers into string representation forms of specific advancement (2, 8, 10, and 16). However, if you need to use a custom character to convert to any advancement, this method will seem powerless.

Custom conversion in advance Convert.ToString

In order to overcome this limit, we can create a custom tool function, such as Method:

Use array buffer to improve performance IntToString

<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
For larger input values, the use of array buffer instead of string connection can significantly improve performance:

Example

The above is the detailed content of How to Efficiently Convert a Base 10 Integer to Any Base in C#?. 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