It takes an input string (input
), splits it on the space character (input.Split(' ')
) (presumably to get the number of “words”), adds 1 to the .Length
of the resulting array (not sure why), converts that number to a binary string (Convert.ToString(int, 2)
will convert the int to a base-2 number and return it as a string), then pads the left side of the string with the 0
character until it’s 8
characters long (.PadLeft(8, '0')
).
My guess is that this might be used in some kind of encoding/decoding algorithm(?).
Here it is in action:
var inputStrings = new List<string>
{
"one",
"two words",
"this is three",
"this one is four",
"and this one has five"
};
foreach(var input in inputStrings)
{
var result = Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0');
Console.WriteLine($"{input.PadRight(22, ' ')} = {result}");
}
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
Output
solved Programming with C# console application