CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2006
    Location
    Bangalore
    Posts
    47

    [RESOLVED] How to convert integer to string in C#

    Hello all.
    I am developing a code in which I need to convert int to string. Plz help...

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to convert integer to string in C#

    Hi,

    Like this:

    Code:
    String number;
    int x = 10;
    number = x.ToString();
    Regards,

    Laitinen

  3. #3
    Join Date
    Sep 2006
    Posts
    392

    Re: How to convert integer to string in C#

    Or
    Code:
                int x = 10;
                string xstring = Convert.ToString(x);
                MessageBox.Show(xstring);
    VS 2005

  4. #4
    Join Date
    Sep 2006
    Location
    Bangalore
    Posts
    47

    Re: How to convert integer to string in C#

    This is the code...

    asset_file_list += "\n<asset size=" + asset_file_size + " count=" + asset_file_count + ">\n";

    here i want that "asset_file_list" variable should get the value like

    <asset size="517361" count="11">

    but its giving...

    <asset size=517361 count=11>

    Plz check and give support...

  5. #5
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to convert integer to string in C#

    Code:
    asset_file_list += "\n<asset size=" + asset_file_size.ToString() + " count=" + asset_file_count.ToString() + ">\n";

  6. #6
    Join Date
    Sep 2006
    Location
    Bangalore
    Posts
    47

    Re: How to convert integer to string in C#

    This is not working...Giving the same output as above mensioned. I tried this earlier...

  7. #7
    Join Date
    Feb 2007
    Posts
    43

    Re: How to convert integer to string in C#

    Do you really want the output to look like this:
    <asset size="517361" count="11">

    assuming that asset_file_size and asset_file_count is already a string,
    Code:
    asset_file_list += "\n<asset size="  + "\"" + asset_file_size + "\""  + " count=" + "\""+ asset_file_count + "\"" + ">\n";
    otherwise do this:

    Code:
    asset_file_list += "\n<asset size="  + "\"" + asset_file_size.ToString() + "\""  + " count=" + "\""+ asset_file_count.ToString() + "\"" + ">\n";

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

    Re: How to convert integer to string in C#

    you've got to wonder how some folks make it out of bed given gravity's strong pull...

  9. #9
    Join Date
    Sep 2006
    Location
    Bangalore
    Posts
    47

    Re: How to convert integer to string in C#

    When I am using
    -----------------------------------------------------------------------------------------------------
    asset_file_list += "\n<asset size=" + "\"" + asset_file_size + "\"" + " count=" + "\""+ asset_file_count + "\"" + ">\n";
    ----------------------------------------------------------------------------------------------------

    I am getting the output:

    "\n<asset size=\"10911\" count=\"1\">\n"

    When I am using
    -----------------------------------------------------------------------------------------------------
    asset_file_list += "\n<asset size=" + "\"" + asset_file_size.ToString() + "\"" + " count=" + "\""+ asset_file_count.ToString() + "\"" + ">\n";
    ----------------------------------------------------------------------------------------------------

    I am getting the output:

    "\n<asset size=\"10911\" count=\"1\">\n"



    Expected output:

    <asset size="10911" count="1">

  10. #10
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to convert integer to string in C#

    Where are you getting that output? If you see it in the debug window, then you have to take a look at the "Text Visualizer" and I am sure you will get the expected result.

    This code works perfect in a multiline textbox;

    Code:
    string asset_file_list = "";
    int asset_file_size = 517361; 
    int asset_file_count = 11;
    asset_file_list += "\r\n<asset size=\"" + asset_file_size.ToString() + "\" count=\"" + asset_file_count.ToString() + "\">\r\n";
    textBox1.Text = asset_file_list;
    Regards,

    Laitinen
    Last edited by laitinen; March 16th, 2007 at 02:57 AM.

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

    Re: How to convert integer to string in C#

    are you perhaps looking in the watch window and not at your actual output???

  12. #12
    Join Date
    Sep 2006
    Location
    Bangalore
    Posts
    47

    Re: How to convert integer to string in C#

    Thank you all...Problem is solved...

  13. #13
    Join Date
    Aug 2013
    Posts
    2

    Re: [RESOLVED] How to convert integer to string in C#

    For anyone who's curious about the fastest way to convert an int to a string, you can read this webpage (http://cc.davelozinski.com/c-sharp/f...-int-to-string).

    Basically it's a toss up between:

    .ToString()
    Convert.ToString()

    until you get to about 21 million conversions.

    The code is there too if you want to run it on your own system.

    Good luck!
    Last edited by FireMyst; July 5th, 2014 at 07:27 AM. Reason: Updated link since it was out of date.

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