## Convert int to string?

### Question

How can I convert an `int` datatype into a `string` datatype in C#?

2014/06/09
1
509
6/9/2014 4:33:03 AM

``````string myString = myInt.ToString();
``````
2020/03/14
751
3/14/2020 1:20:30 AM

Just in case you want the binary representation and you're still drunk from last nights party:

``````private static string ByteToString(int value)
{
StringBuilder builder = new StringBuilder(sizeof(byte) * 8);
BitArray[] bitArrays = BitConverter.GetBytes(value).Reverse().Select(b => new BitArray(new []{b})).ToArray();
foreach (bool bit in bitArrays.SelectMany(bitArray => bitArray.Cast<bool>().Reverse()))
{
builder.Append(bit ? '1' : '0');
}
return builder.ToString();
}
``````

Note: Something about not handling endianness very nicely...

Edit: If you don't mind sacrificing a bit of memory for speed, you can use below to generate an array with pre-calculated string values:

``````static void OutputIntegerStringRepresentations()
{
Console.WriteLine("private static string[] integerAsDecimal = new [] {");
for (int i = int.MinValue; i < int.MaxValue; i++)
{
Console.WriteLine("\t\"{0}\",", i);
}
Console.WriteLine("\t\"{0}\"", int.MaxValue);
Console.WriteLine("}");
}
``````
2017/07/12

``````int num = 10;
string str = Convert.ToString(num);
``````
2010/06/21

The ToString method of any object is supposed to return a string representation of that object.

``````int var1 = 2;

string var2 = var1.ToString();
``````
2010/06/21

Further on to @Xavier's response, here's a page that does speed comparisons between several different ways to do the conversion from 100 iterations up to 21,474,836 iterations.

It seems pretty much a tie between:

``````int someInt = 0;
someInt.ToString(); //this was fastest half the time
//and
Convert.ToString(someInt); //this was the fastest the other half the time
``````
2014/09/06

``````string str = intVar.ToString();
``````

In some conditions, yo do not have to use `ToString()`

``````string str = "hi " + intVar;
``````
2013/05/25
{"ft_lang_label":"__label__en","ft_lang_prob":0.64124197,"math_prob":0.8253757,"size":2155,"snap":"2021-43-2021-49","text_gpt3_token_len":526,"char_repetition_ratio":0.15202232,"word_repetition_ratio":0.006802721,"special_character_ratio":0.2974478,"punctuation_ratio":0.21271394,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9595347,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-26T01:33:43Z\",\"WARC-Record-ID\":\"<urn:uuid:0c9dc0db-c0b1-4a20-bbbe-eebb87f1c247>\",\"Content-Length\":\"29871\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:93656553-4cb4-43cb-abe1-9416fb2cdefb>\",\"WARC-Concurrent-To\":\"<urn:uuid:acd53b35-000c-4e66-8ca4-61dd5ac724e6>\",\"WARC-IP-Address\":\"104.21.8.31\",\"WARC-Target-URI\":\"https://ask4knowledgebase.com/questions/3081916/convert-int-to-string-\",\"WARC-Payload-Digest\":\"sha1:YVWCC3JS7VFXAZPOD7NP2JPZOBQNJUJY\",\"WARC-Block-Digest\":\"sha1:RIEX7CWGP3AGDHIJKC2BCH4OAR2KGLUH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587794.19_warc_CC-MAIN-20211026011138-20211026041138-00355.warc.gz\"}"}