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.

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.