Use the BitConverter class to write extension methods to convert between byte arrays and strings of hexadecimal values in C#




This is a new version of the example Write extension methods to convert between byte arrays and strings of hexadecimal values in C# that takes a slightly different approach.

The following code shows the new versions of the extension methods.

public static class StringExtensions
{
// Return a string that represents the byte array
// as a series of hexadecimal values separated
// by a separator character.
public static string ToHex(this byte[] the_bytes)
{
return BitConverter.ToString(the_bytes, 0);
}
// Convert a string containing 2-digit hexadecimal
// values into a byte array.
public static byte[] ToBytes(this string the_string)
{
// Split at the separators.
string[] pairs = the_string.Split('-');
byte[] bytes = new byte[pairs.Length];
for (int i = 0; i < pairs.Length; i++)
bytes[i] = Convert.ToByte(pairs[i], 16);
return bytes;
}
}

The ToHex method simply uses the BitConverter class’s ToString method to convert the byte array into a string of the form 54-00-6F-00-20. (If you don’t like the hyphens, you can use the string class’s Replace method to convert them into spaces or remove them entirely.) (The previous example looped through the bytes converting each into its hexadecimal for so the new version is a lot shorter.)

Unfortunately the BitConverterr class doesn’t have a reverse method that converts a string of the form 54-00-6F-00-20 back into a byte array, so you need to write your own. Fortunately, that’s not hard.

The ToBytes method splits the string at the hyphens to get an array of two-character pairs. It makes an array to hold the byte values and loops through the pairs, using Convert.ToByte to convert the pairs into byte values. (The previous example used byte.Parse to convert the pairs into byte values. That’s slightly more verbose because you have to specify the System.Globalization.NumberStyles.HexNumber style instead of the value 16 to indicate that the pairs hold hexadecimal values.)

This example also uses new code to convert strings to and from byte arrays. The previous example used the ASCIIEncoding class. The new example uses the UnicodeEncoding class. The following code shows how the main program converts strings to and from byte arrays. The extension methods are highlighted in bold.

// Convert the string into an array of bytes and display it.
private void btnToHexString_Click(object sender, EventArgs e)
{
// Convert the string into bytes.
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] bytes = encoding.GetBytes(txtOriginal.Text);
// Display the result as a string of hexadecimal values.
txtHexadecimal.Text = bytes.ToHex();
}
// Converrt the string of hexadecimal values back into a string.
private void btnToString_Click(object sender, EventArgs e)
{
// Convert the string of hexadecimal values into an array of bytes.
byte[] bytes = txtHexadecimal.Text.ToBytes();
// Convert the bytes into a string and display the result.
UnicodeEncoding encoding = new UnicodeEncoding();
txtConvertedBack.Text = encoding.GetString(bytes);
}

The older example doesn’t handle all Unicode characters because it uses the ASCIIEncoding class instead of the UnicodeEncoding class, so the newer example is better in that respect. The ToHex and ToBytes extension methods work in both examples, although the newer version is slightly more concise.

   

This entry was posted in extension methods, extensions, strings. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.