O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Get a String Representation of a Byte Array

Problem

You need to convert a byte array into a string representation.

Solution

If you are creating a string representation of arbitrary binary data, use BitConverter.ToString or Convert.ToBase64String. If you are restoring text stored in binary format, call the GetString method of the appropriate encoding object in the System.Text namespace.

Discussion

There are several solutions to this time-honored problem, depending on the task you need to accomplish. The quickest approach is to use the System.BitConverter class, which provides shared methods for converting basic data types into byte arrays and vice versa. In this case, you simply need to use the overloaded ToString method that accepts a byte array.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required