.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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds