ZetCode

C# int to string conversion

last modified November 6, 2026

In this article we show how to convert integers to strings. There are several ways to perform int to string conversion in C#. We can use string concatenation, string formatting, string building, and built-in conversion methods.

C# int to string conversion

Integer to string conversion is a type conversion, where an entity of integer data type is changed into a string one. All of the methods shown below eventually call the Int32.ToString method.

In the examples of this article we build string messages that contain an integer.

C# int to string with Int32.ToString

The Int32.ToString method converts the numeric value to its equivalent string representation. The int keyword is an alias for the System.Int32 type, so int.ToString and Int32.ToString are the same method.

Program.cs
int val = 4;

string msg = "There are " + val.ToString() + " hawks";
Console.WriteLine(msg);

int big = -1250;
string s = big.ToString();

Console.WriteLine(s);
Console.WriteLine(s.Length);

The example uses int.ToString to do int to string conversion. The minus sign of a negative number is part of the resulting string.

$ dotnet run
There are 4 hawks
-1250
5

C# int to string with string concatenation

When we use the + operator on int and string operands, the C# compiler converts the integer to a string automatically.

Program.cs
int numOfApples = 16;

string msg = "There are " + numOfApples + " apples";
Console.WriteLine(msg);

Console.WriteLine("Sum: " + 2 + 3);
Console.WriteLine("Sum: " + (2 + 3));
Console.WriteLine(2 + 3 + " is the sum");

The example uses string concatenation to do int to string conversion.

Console.WriteLine("Sum: " + 2 + 3);
Console.WriteLine("Sum: " + (2 + 3));
Console.WriteLine(2 + 3 + " is the sum");

The + operator is evaluated from left to right. Once one of the operands is a string, the following integers are appended as text. To add the numbers first, we use parentheses.

$ dotnet run
There are 16 apples
Sum: 23
Sum: 5
5 is the sum

C# int to string with interpolation and formatting

The following example provides other ways to the int to string conversion in C#.

Program.cs
int val = 4;

string msg = "There are " + Convert.ToString(val) + " hawks";
string msg2 = string.Format("There are {0} hawks", val);
string msg3 = $"There are {val} hawks";
string msg4 = string.Concat("There are ", val, " hawks");

Console.WriteLine(msg);
Console.WriteLine(msg2);
Console.WriteLine(msg3);
Console.WriteLine(msg4);

We use Convert.ToString, string.Format, string interpolation, and string.Concat to do int to string conversions. String interpolation is the most readable option and the compiler translates it into efficient code which does not box the integer.

$ dotnet run
There are 4 hawks
There are 4 hawks
There are 4 hawks
There are 4 hawks

C# int to string with StringBuilder

StringBuilder represents a mutable string of characters. We can use StringBuilder to construct strings. We can append integers to the builder as well.

Program.cs
using System.Text;

int val = 4;

var builder = new StringBuilder();

builder.Append("There are ");
builder.Append(val);
builder.Append(" hawks");

Console.WriteLine(builder.ToString());

builder.Clear();

int[] vals = [1, 2, 3, 4, 5];

foreach (var e in vals)
{
    builder.Append(e).Append(' ');
}

Console.WriteLine(builder.ToString().TrimEnd());

The code example uses StringBuilder to do int to string conversion.

builder.Append(val);

The Append method has an overload for int; the number is written directly into the builder's buffer. The Append method returns the builder, so the calls can be chained.

$ dotnet run
There are 4 hawks
1 2 3 4 5

C# int to string with format specifiers

The ToString method and interpolation holes accept format specifiers, which control how the number looks.

Program.cs
using System.Globalization;

int val = 1234567;
int small = 42;

Console.WriteLine(small.ToString("D5"));
Console.WriteLine(val.ToString("N0", CultureInfo.InvariantCulture));
Console.WriteLine(val.ToString("N0", new CultureInfo("de-DE")));
Console.WriteLine(small.ToString("X"));
Console.WriteLine(small.ToString("x4"));
Console.WriteLine(val.ToString("E2", CultureInfo.InvariantCulture));

Console.WriteLine($"[{small,6}]");
Console.WriteLine($"[{small,-6}]");
Console.WriteLine($"[{small:D4}]");

We format two integers with various specifiers.

Console.WriteLine(small.ToString("D5"));

The D specifier formats a decimal integer; the number pads it with leading zeros to the given width.

Console.WriteLine(val.ToString("N0", CultureInfo.InvariantCulture));
Console.WriteLine(val.ToString("N0", new CultureInfo("de-DE")));

The N specifier adds group separators. The separator depends on the culture. The number after N sets the number of decimal places.

Console.WriteLine(small.ToString("X"));
Console.WriteLine(small.ToString("x4"));

The X and x specifiers produce uppercase and lowercase hexadecimal digits.

Console.WriteLine($"[{small,6}]");
Console.WriteLine($"[{small,-6}]");
Console.WriteLine($"[{small:D4}]");

In an interpolation hole, the number after the comma sets the field width; a negative width aligns the value to the left. The format specifier follows the colon. See the String Format article for more details.

$ dotnet run
00042
1,234,567
1.234.567
2A
002a
1.23E+006
[    42]
[42    ]
[0042]

C# int to string and culture

Without a format provider, ToString uses the current culture. For integers, this matters mostly for the negative sign.

Program.cs
using System.Globalization;

int val = -5;

CultureInfo.CurrentCulture = new CultureInfo("sv-SE");

string s1 = val.ToString();
string s2 = val.ToString(CultureInfo.InvariantCulture);

Console.WriteLine($"{s1} has length {s1.Length}, first char U+{(int)s1[0]:X4}");
Console.WriteLine($"{s2} has length {s2.Length}, first char U+{(int)s2[0]:X4}");

Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $"Value: {val}"));

We set the current culture to Swedish and convert a negative number.

string s1 = val.ToString();
string s2 = val.ToString(CultureInfo.InvariantCulture);

With the ICU library used by .NET on Linux and macOS, as well as on recent Windows versions, the Swedish culture uses the Unicode minus sign U+2212 instead of the ASCII hyphen-minus. Such a string cannot be parsed back with the invariant culture. Strings meant for files, protocols, or other programs should be created with CultureInfo.InvariantCulture.

Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $"Value: {val}"));

The string.Create method performs string interpolation with the given culture.

$ dotnet run
−5 has length 2, first char U+2212
-5 has length 2, first char U+002D
Value: -5

C# int to string in other bases

The Convert.ToString method can convert an integer to its binary, octal, or hexadecimal representation. Since .NET 8, the B format specifier produces binary output.

Program.cs
int val = 42;

Console.WriteLine(Convert.ToString(val, 2));
Console.WriteLine(Convert.ToString(val, 8));
Console.WriteLine(Convert.ToString(val, 16));

Console.WriteLine(val.ToString("B8"));
Console.WriteLine($"{val:B}");

Console.WriteLine(Convert.ToString(-1, 2));

We convert the number 42 to base 2, 8, and 16.

Console.WriteLine(val.ToString("B8"));

The B specifier also supports padding to a given width.

Console.WriteLine(Convert.ToString(-1, 2));

Negative numbers are shown in two's complement representation; all 32 bits of -1 are set.

$ dotnet run
101010
52
2a
00101010
101010
11111111111111111111111111111111

C# int to string with TryFormat

The TryFormat method writes the number into a span of characters without allocating a new string. It is used in performance-critical code.

Program.cs
int val = 2026;

Span<char> buffer = stackalloc char[16];

if (val.TryFormat(buffer, out int written, "D6"))
{
    ReadOnlySpan<char> res = buffer[..written];
    Console.WriteLine($"{res} ({written} characters)");
}

Span<char> tiny = stackalloc char[2];
Console.WriteLine(val.TryFormat(tiny, out _));

We format the number into a buffer allocated on the stack.

if (val.TryFormat(buffer, out int written, "D6"))

The method returns true on success and the number of written characters in the out parameter.

Span<char> tiny = stackalloc char[2];
Console.WriteLine(val.TryFormat(tiny, out _));

If the buffer is too small, the method returns false instead of throwing an exception.

$ dotnet run
002026 (6 characters)
False

C# int array to string

To convert a whole collection of integers to a single string, we use string.Join or string.Concat.

Program.cs
int[] vals = [3, 1, 4, 1, 5, 9, 2, 6];

string s1 = string.Join(", ", vals);
string s2 = string.Concat(vals);
string[] parts = vals.Select(e => e.ToString("D2")).ToArray();

Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(string.Join(' ', parts));

The string.Join method converts each integer to a string and puts the given separator between them; string.Concat joins them without a separator. With LINQ's Select, we can format each number before joining.

$ dotnet run
3, 1, 4, 1, 5, 9, 2, 6
31415926
03 01 04 01 05 09 02 06

Source

Int32.ToString method

In this article we have shown how to perform int to string conversions in C#.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all C# tutorials.