Title: Make a program display its own binary code in three ways in C#
I recently wanted to put a chunk of binary code in a book to show what code really looks like to the computer, so I wrote this program. This isn't a spectacularly useful program, but it does demonstrate a few useful string processing techniques.
When the program starts, it executes the following code.
private void Form1_Load(object sender, EventArgs e)
{
byte[] bytes = File.ReadAllBytes(Application.ExecutablePath);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
string value = Convert.ToString(bytes[i], 2);
value = value.PadLeft(8, '0');
sb.Append(value + ' ');
}
txtResult.Text = sb.ToString();
}
The code gets the application's executable path (the full path to the running .exe file) and uses the File.ReadAllBytes method to read its contents into an byte array.
The code then loops through the bytes. It uses Convert.ToString to convert each byte into a binary representation and uses the string's PadLeft method to add 0s on the left to make the result take up 8 characters. It adds the byte's representation to the string txt followed by a space.
When it's finished, the program displays the result.
The File.ReadAllBytes, Convert.ToString, and PadLeft methods make this relatively easy, but there's another useful method that can make the code even shorter by eliminating the for loop. The following code does the same thing as the original code.
byte[] bytes = File.ReadAllBytes(Application.ExecutablePath);
string[] strings = Array.ConvertAll(bytes,
b => Convert.ToString(b, 2).PadLeft(8, '0'));
txtResult.Text = string.Join(" ", strings);
This version uses File.ReadAllBytes as before to read the executable file's bytes into an array.
The code then uses Array.ConvertAll to convert the values in the array from bytes into strings. The Array.ConvertAll method takes as parameters the array to convert and a converter function. This example uses an expression lambda to convert the input b into a string. The lambda calls Convert.ToString for the value b, and then calls the resulting string's PadLeft method.
When Array.ConvertAll finishes, it returns the bytes converted into an array of strings. The code calls the string.Join method to join the strings in the array together separated by space characters. The code then displays the result.
At this point, you have two ways to make the program display its binary code. Just for fun, I decided to add a LINQ solution. The following code shows one way you can use LINQ to show the program's code.
var query =
from byte b in File.ReadAllBytes(Application.ExecutablePath)
select Convert.ToString(b, 2).PadLeft(8, '0');
txtResult.Text = string.Join(" ", query.ToArray());
This code declares a LINQ query that iterates over the bytes returned by the File.ReadAllBytes method. For each byte, the query selects the result returned by Convert.ToString followed by PadLeft. The code then invokes the query's ToArray method to execute the query and save the results in an array. It uses string.Join to convert the array of strings into a single string and displays the result.
Note that all three versions take about the same amount of time. (Although if you concatenate strings onto a string variable instead of using a StringBuilder, the first solution takes about twice as long as the others.)
Download the example to experiment with it and to see additional details.
|