Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an Int to a String in C# without using ToString()?

Convert the following int argument into a string without using any native toString functionality.

public string integerToString(int integerPassedIn){         //Your code here } 

Since everything inherits from Object and Object has a ToString() method how would you convert an int to a string without using the native ToString() method?

The problem with string concatenation is that it will call ToString() up the chain until it hits one or hits the Object class.

How do you convert an integer to a string in C# without using ToString()?

like image 750
Anthony Russell Avatar asked Jul 10 '13 15:07

Anthony Russell


People also ask

Is the easiest way to convert an integer to a string in C?

The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf().

What is atoi () in C?

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.


2 Answers

Something like this:

public string IntToString(int a) {         var chars = new[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };     var str = string.Empty;     if (a == 0)     {         str = chars[0];     }     else if (a == int.MinValue)     {         str = "-2147483648";     }     else     {         bool isNegative = (a < 0);         if (isNegative)         {             a = -a;         }          while (a > 0)         {             str = chars[a % 10] + str;             a /= 10;         }          if (isNegative)         {             str = "-" + str;         }     }      return str; } 

Update: Here's another version which is shorter and should perform much better, since it eliminates all string concatenation in favor of manipulating a fixed-length array. It supports bases up to 16, but it would be easy to extend it to higher bases. It could probably be improved further:

public string IntToString(int a, int radix) {     var chars = "0123456789ABCDEF".ToCharArray();     var str = new char[32]; // maximum number of chars in any base     var i = str.Length;     bool isNegative = (a < 0);     if (a <= 0) // handles 0 and int.MinValue special cases     {         str[--i] = chars[-(a % radix)];         a = -(a / radix);     }      while (a != 0)     {         str[--i] = chars[a % radix];         a /= radix;     }      if (isNegative)     {         str[--i] = '-';     }      return new string(str, i, str.Length - i); } 
like image 114
p.s.w.g Avatar answered Oct 11 '22 04:10

p.s.w.g


This is the solution I always use:

    public static string numberBaseChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";      public static string IntToStringWithBase(int n, int b) {         return IntToStringWithBase(n, b, 1);     }      public static string IntToStringWithBase(int n, int b, int minDigits) {         if (minDigits < 1) minDigits = 1;         if (n == 0) return new string('0', minDigits);         string s = "";         if ((b < 2) || (b > numberBaseChars.Length)) return s;         bool neg = false;         if ((b == 10) && (n < 0)) { neg = true; n = -n; }         uint N = (uint)n;         uint B = (uint)b;         while ((N > 0) | (minDigits-- > 0)) {             s = numberBaseChars[(int)(N % B)] + s;             N /= B;         }         if (neg) s = "-" + s;         return s;     } 

This looks quite complicated but has the following features:

  • Supports radix 2 to 36
  • Handles negative values
  • Optional total number of digits
like image 34
joe Avatar answered Oct 11 '22 04:10

joe