CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Sep 2008
    Posts
    5

    Converting int to string

    Hi Guys..
    I Wanted to convert an int to a string in C#...but i cannot use any built in functions.So, can any one tell me an algorithm or a program to do so....please is urgent....

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Converting int to string

    you cant. just use myInt.ToString()

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Converting int to string

    Sounds like Problem Sheet 1 of 1st year computer science.

    Ask your classmates, or ask your professor for help.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Converting int to string

    Quote Originally Posted by Mutant_Fruit
    Sounds like Problem Sheet 1 of 1st year computer science.

    Ask your classmates, or ask your professor for help.
    Totally, I always HATED those kinds of problems. Especially when they ask you to do it in a language like C# that already has those types of conversions easily made with the call of ToString().

    I think if they want you to learn how its done on the inside, teach C or C++, but if they want you to learn how its done in the real world with languages like C# or VB.NET, teach them the benefits of using functions and members that are already provided in those languages.

    Because nobody manually converts a numerical value to a string in C#, not anyone that I know of... The only way I think it can be done is by converting the integer into bytes, and then converting the bytes into a string. But that STILL requires built in encoding methods. So without the use of pointers, its nigh impossible in C#.
    Last edited by RaleTheBlade; September 19th, 2008 at 03:41 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Converting int to string

    Quote Originally Posted by RaleTheBlade
    Totally, I always HATED those kinds of problems. Especially when they ask you to do it in a language like C# that already has those types of conversions easily made with the call of ToString().
    The point of the exercise is to teach you to think. Why else would you want someone to implement a list, or a queue. It's just an easy (ish) practice problem. The only way to get good is to program, so give em something simple to do.

    The only way I think it can be done is by converting the integer into a bytes, and then converting the bytes into strings. But that STILL requires built in encoding methods. So without the use of pointers, its nigh impossible in C#
    There's *much* easier ways to do it. It can be done in 8 lines of code including getting the input number and printing the result to screen. No pointers needed. Of course, it's up to the original poster to come up with that solution

    EDIT: If you converted the integer into bytes and converted bytes to string, you'd have to use a HEX representation or do some fairly nasty bit-mangling
    Last edited by Mutant_Fruit; September 19th, 2008 at 03:47 PM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Jan 2007
    Posts
    491

    Re: Converting int to string

    As mentioned earlier, you could use the int.ToString() or Convert.ToString(int) methods.
    Here's my own method to do that:
    Code:
    IntToString(int n)
    {
        StringBuilder sb;
        string result = null;
        if(n == 0)
            return "0";
        using(sb = new StringBuilder())
        {
            if(n < 0)
                sb.Append('-');
            
            while(n != 0)
            {
                sb.Append((char)(n%10 + 48));
                n /= 10;
            }
            sb.Reverse();
            result = sb.ToString();
        }
        return result;
    }

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: Converting int to string

    And now we have another compsci student who has learnt the most important technique for solving problems, the art of Copy&Paste.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Converting int to string

    technically speaking, that's still using built in functionality...

  9. #9
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Talking Re: Converting int to string

    1.ToString();
    2.ToString();

    weird!!! it works also because all types inherits from Obejct that it has ToString() method....so you see ToString() method is everywhere in c# even in the hole of a pin or under the leaves of a bean!

    C# is completely an Object Oriented Language and strongly typed.

  10. #10
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Converting int to string

    Quote Originally Posted by toraj58
    1.ToString();
    2.ToString();

    weird!!! it works also because all types inherits from Obejct that it has ToString() method....so you see ToString() method is everywhere in c# even in the hole of a pin or under the leaves of a bean!

    C# is completely an Object Oriented Language and strongly typed.
    .ToString() is built-in functionality....therefore, it does not apply to his problem.

  11. #11
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Talking Re: Converting int to string

    Quote Originally Posted by eclipsed4utoo
    .ToString() is built-in functionality....therefore, it does not apply to his problem.
    I know my answer was complementary of others; my friend!

  12. #12
    Join Date
    Apr 2006
    Posts
    220

    Re: Converting int to string

    Algorithm may be something like this

    1- Get every digit of the input integer
    2- Add the above digit in some char[]
    3- Use typecasting for converting int (digit) to char (for char[])
    4- When all digits are finished, your resulting string i.e char[] is ready.

  13. #13
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Converting int to string

    Such school tasks are senseless like hell, especially for C#.

    Instead learning how to code and implement clean .NET apps,
    teachers force students to do such crap. Why the hell
    the programming languages are so powerful?!

    It has nothing to do with programming skills to be able to code
    an IntToString by your own... it is sick. Such tasks are only useful if you are using pure assembler.

    ...but this is C#... heeeelllloooooo?????
    There are billions of other problems to solve than such crap.


    Ohhh wait..... why the teacher does not force to build our own cars?! *dough!*


    Note for teachers. These are some example which can be implemented:
    - IntToBinaryString
    - BinayStringToInt
    - Calculating slopes (y=mx+b)
    - Formula parser
    ....

  14. #14
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Converting int to string

    It isn't useless to solve such a task for the reason which Mutant Fruit has pointed out. It is useless to suck the solution from others instead to solve it itself, and it is useless to discourage student with talks about useless of learning trought solving problem.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  15. #15
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Wink Re: Converting int to string

    and it is useless to discourage student with talks about useless of learning trought solving problem
    Life is hard.
    Welcome to the real world! ;-)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured