.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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds