Format string

I usually try to comment and log a lot when I write a program, I never regret it. But in order to write information that is actually meaningful you need to get the runtime context, like the current value of some variables or what exception has been thrown. Furthermore you will want to facilitate comprehension for the reader, if you work with hexadecimal values, you will also want to show your value as hexadecimal and spare the reader the conversion. Here are some of the most useful functions for formatting your strings and allowing you to easily write messages that matter and that are comprehensive.

Formatting strings

$ – string interpolation

String Interpolation is available since C# 6, with it you can compose a string with other variables and make your code actually fairly readable for people.

int value = 255;
Console.WriteLine($"The hexadecimal value of {value} is 0x{value.ToString("X")}");
The hexadecimal value of 255 is 0xFF

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

@ – verbatim string literal

string filename1 = @"C:\Windows\System32\shell32.dll";
string filename2 = "C:\\Windows\\System32\\shell32.dll";
Console.WriteLine(filename1);
Console.WriteLine(filename2);
C:\Windows\System32\shell32.dll
C:\Windows\System32\shell32.dll

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim

Formatting numbers

The ToString() method accepts a string as Parameter, it consist of a letter, big or small and optionally an associated number, which sets how many letters the resulting string should at least have.

X – hexadecimal

int intValue = 255;
// hexadecimal format
Console.WriteLine(intValue.ToString("x"));
Console.WriteLine(intValue.ToString("X4"));
ff
00FF

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

F – fixed-point

int intValue = 255;
double doubleValue = 1234.5678;
// fixed-point format
Console.WriteLine(intValue.ToString("F"));
Console.WriteLine(doubleValue.ToString("F"));
Console.WriteLine(doubleValue.ToString("F3"));
255.00
1234.56
1234.567

0 – zero placeholder

double doubleValue = 1234.5678;
// 0 placeholder
Console.WriteLine(doubleValue.ToString("000"));
Console.WriteLine(doubleValue.ToString("00000.00"));
Console.WriteLine(doubleValue.ToString("00000.00000"));
1235
01234,57
01234,56780

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

# – digit placeholder

double doubleValue = 1234.5678;
// digit placeholder
Console.WriteLine(doubleValue.ToString("###"));
Console.WriteLine(doubleValue.ToString("#####.##"));
Console.WriteLine(doubleValue.ToString("#####.#####"));
1235
1234,57
1234,5678

Convert – string to int

// base 10 string
string intValue = "64";
Console.WriteLine(Convert.ToInt32(intValue));

// base 16 string
string hexValueAsString = "0x64";
Console.WriteLine(Convert.ToInt32(hexValueAsString, 16));
64
100

https://docs.microsoft.com/de-de/dotnet/api/system.convert.tostring?view=netcore-3.1#System_Convert_ToString_System_Int16_System_Int32_

BitConverter – int to byte array

int value = 511;
byte[] bytes = BitConverter.GetBytes(value);
Console.WriteLine(BitConverter.ToString(byteArray));
127-01-00-00

https://docs.microsoft.com/de-de/dotnet/api/system.bitconverter.getbytes?view=netcore-3.1#System_BitConverter_GetBytes_System_Int32_

string.Join() – Array to string

int[] values = new int[] { 1, 2, 3, 4, 255 };
Console.WriteLine(string.Join(",", values));
Console.WriteLine(string.Join(",", values.Select(b => b.ToString("X2"))));
1,2,3,4,255
01,02,03,04,FF

https://docs.microsoft.com/de-de/dotnet/api/system.string.join?view=netcore-3.1#System_String_Join_System_String_System_Collections_Generic_IEnumerable_System_String__

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s