I recently covered ways to convert a string into a byte array with VB.NET. When you need to convert a byte array into a string, you can use either BitConverter.ToString or Convert.ToBase64String methods. I provide examples that show you how to make the conversion using both methods.
Byte array usage
If you are restoring text stored in binary format, call the GetString method of the appropriate encoding object in the System.Text namespace.The quickest way to convert a byte array into a string is to use the System.BitConverter class. The class provides methods for converting basic data types into byte arrays and from byte arrays. To use this method, use the overloaded ToString method that accepts a byte array as a parameter. In this case, the string contains each value of the byte array in hexadecimal format separated by a dash. There is no automatic way to reverse the conversion to find out the original byte array using the string. View the example in Listing A. Another option for converting a byte array into a string is to utilize Base64 encoding through the ToBase64String and FromBase64String methods of the System.Convert class. In Base64 encoding, each sequence of three bytes is converted into a separate of four types. Each Base64 encoded character has one of the 64 possible values in the range. View the example in Listing B.
Both options are useful for creating a representation of binary data. Keep in mind that, in order to retrieve a real text information from a byte array, you have to use the correct encoding class.
Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.
————————————————————————————————————————————Get Visual Basic tips in your inbox
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!