.NET Tip: Take a Byte out of Strings

If you work much with Streams or Sockets, you are bound come across the need to convert a string to a byte array or to convert a byte array into a string. There is an easy means to accomplish this, but it is not where I would have expected it to be. The methods to do this are found in the Encoding class. The GetBytes() method is used to get a byte array representation of a string and the GetString() method is used to get a string representation of an array of bytes. Here is an example of how to use the methods:

string s1 = "This is a test of converting a string to a
             byte array and back again.";
string s2 = "";
byte[] b;

b = Encoding.ASCII.GetBytes(s1);
s2 = Encoding.ASCII.GetString(b);

Debug.Print("Byte array length: {0}", b.Length);
Debug.Print("Strings are equal: {0}", s1 == s2);

Start with the string to convert, in this case s1. The code then uses GetBytes() to turn the string into a byte array. The byte array is then turned back into a string using GetString(), which is stored in s2. The Debug.Print() statements display the length of the byte array to show that it is the same length as the string and a comparison of s1 and s2 to show that they are the same. The above example uses ASCII encoding. If you are working with Unicode characters, you should use the Unicode version of the methods in the Encoding class as shown below.

b = Encoding.Unicode.GetBytes(s1);
s2 = Encoding.Unicode.GetString(b);

Debug.Print("Byte array length: {0}", b.Length);
Debug.Print("Strings are equal: {0}", s1 == s2)

You will find that the length of the byte array is now twice as long because each Unicode character requires two bytes of storage.

There is one more thing that I think could be done to make the code a little cleaner and you won't have to remember that the GetBytes() and GetSTring() methods are in the Encoding class. Create extension methods that take care of the conversion for you. Here are examples of extension methods that add GetBytes() and GetUnicodeBytes() methods to strings as well as GetString() and GetUnicodeString() methods to bytes.

public static byte[] GetBytes(this string s)
{
    return (Encoding.ASCII.GetBytes(s));
}

public static byte[] GetUnicodeBytes(this string s)
{
    return (Encoding.Unicode.GetBytes(s));
}

public static string GetString(this byte[] b)
{
    return (Encoding.ASCII.GetString(b));
}

public static string GetUnicodeString(this byte[] b)
{
    return (Encoding.Unicode.GetString(b));
}

Using the extension methods instead of the Encoding class directly looks like this:

// ASCII Version
b = s1.GetBytes();
s2 = b.GetString();

// Unicode Version
b = s1.GetUnicodeBytes();
s2 = b.GetUnicodeString();

I hope this helps when you need to work with strings and byte arrays. Remember to take advantage of extension methods when appropriate to help simplify your application.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

  • As everyone scrambles to protect customers and consumers from the Heartbleed virus, there will be a variety of mitigating solutions offered up to address this pesky bug. There are a variety of points within the data path where solutions could be put into place to mitigate this (and similar) vulnerabilities and customers must choose the most strategic point in the network at which to deploy their selected mitigation. Read this white paper to learn the ins and outs of mitigating the risk of Heartbleed and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds