Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 216021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:31:31+00:00 2026-05-11T18:31:31+00:00

I have and old(ish) C# method I wrote that takes a number and converts

  • 0

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);

It’s not all that super speedy and neat. Is there a good, known way of achieving this in .NET?

I’m looking for something that allows me to use any base with an arbitrary string of characters to use.

This only allows bases 16, 10, 8 and 2:

Convert.ToString(1, x);

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.

Does anyone know of a good and efficient way of doing this in C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:31:32+00:00Added an answer on May 11, 2026 at 6:31 pm

    Convert.ToString can be used to convert a number to its equivalent string representation in a specified base.

    Example:

    string binary = Convert.ToString(5, 2); // convert 5 to its binary representation
    Console.WriteLine(binary);              // prints 101
    

    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.

    Update (to meet the requirement to convert to any base):

    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);
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have old Delphi application. This app takes session key from server, do some
I have a question that's so simple I cannot believe I can't answer it
I have a problem concerning Google cache my old content URLs while I created
if try implemnt my first MVVM in WPF app. I have old app based
I recently upgraded my computer, and moved from XP to Win7. We have old
We have a website which has a decent customer base. We recently started a
def foo(a, b, c = 0): return a+b I have dozens of functions like
I have an application in Visual c++ (Win32 API). In my application the main
I have a website which stores customer details, name, address, contact etc. A customer
When using WPF databinding, I obviously can't do something along the lines of MyCollection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.