.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

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds