.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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • It's hardly surprising that half of small businesses fail within the first 1-5 years. It's not easy to launch a new product, single-handedly manage everything from IT to accounting, fend off the competition, and grow a customer base – all at the same time – even with a great concept. Offering awesome customer service can make the difference between a startup that flies and a startup that dies. Read this white paper to learn nine ways customer support can help you beat the competition and grow your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds