Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is the most effective code to convert int to string without using a native convert function.

 public static void Main(string[] args)
    {
        string y = Convert(990);
        Console.WriteLine(y);
        Console.ReadLine(); 
    }


    public static string Convert(int x)
    {

         char[] Str = new char[20];
         int i = 0;
         while (x != 0)
         {
             Str[i++] = x % 10 + '0';
             x = x / 10;
         }

         return Str.ToString();// How do I handle this without the native function?
    }

The above doesnt seem to work. Please advise.

share|improve this question
2  
what? what do you wnat that for? – HighCore Jul 4 at 21:21
This is a school assignment, and therefore not for us to do. – It'sNotALie. Jul 4 at 21:21
2  
What can you use and what can't you use? – Tim Schmelter Jul 4 at 21:26
1  
While an obvious classroom or interview question, at least you posted code. Plus it gave me an excuse to write 'clever' code. Fun occasionally. – Mark Peters Jul 4 at 22:11
add comment (requires an account with 50 reputation)

3 Answers

up vote 1 down vote accepted

Here's a solution without a String constructor or a .ToString() call. This one handles negative numbers. It's a bit too 'clever' for my taste, but it's an academic exercise anyway...

void Main()
{
    Console.WriteLine(Convert(-5432));
}

String Convert(int i)
{
    return String.Join("",Digits(i).Reverse());
}

IEnumerable<char> Digits(int i)
{
    bool neg = false;
    if(i==0) {yield return '0'; yield break;}
    if(i<0) { i = -i; neg = true;}
    while (i!=0)
    {
        char digit = (char)(i % 10 + '0');
        i = i / 10;
        yield return digit;
    }
    if(neg) yield return '-';

    yield break;
}
share|improve this answer
add comment (requires an account with 50 reputation)

It might be easier to construct your result using a StringBuilder rather than a char array:

public static string Convert(int x)
{
     StringBuilder sb = new StringBuilder();

     while (x != 0)
     {
         sb.Insert(0, (char)(x % 10 + '0'));
         x = x / 10;
     }

     return sb.ToString();
}

The Insert(0, x) allows you to insert the more significant digits at the front.

share|improve this answer
What do you think x % 10 + '0' is? – David Heffernan Jul 4 at 21:39
@DavidHeffernan: You're right; I was assuming that that part of the OP's code was correct. Now fixed. – Douglas Jul 4 at 21:43
add comment (requires an account with 50 reputation)

You are adding the least significant digits to the start of the string. You'll need to reverse the string, or add the digits in reverse order, or insert them rather than append them.

And you'll need to do some casting. For example, here's one way to do it:

Str[i++] = (char)(x % 10 + '0');

Finally, you cannot use ToString() like that. You want this:

return new string(Str, 0, i);

Although StringBuilder might be more suitable.

And note that your code won't work properly for negative input values, or for zero.

So, here's a version that handles all of that:

public static string Convert(int x)
{
    if (x == 0)
    {
        return "0";
    }
    StringBuilder sb = new StringBuilder();
    string prefix = "";
    if (x < 0)
    {
        prefix = "-";
        x = -x;
    }
    while (x != 0)
    {
        sb.Insert(0, (char)(x % 10 + '0'));
        x = x / 10;
    }
    return prefix + sb.ToString();
}

This is pretty ugly though!

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.