This documentation is archived and is not being maintained.

BitConverter.ToString Method (Byte[])

Returns a String converted from the elements of a byte array.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static string ToString (
	byte[] value
)
public static String ToString (
	byte[] value
)
public static function ToString (
	value : byte[]
) : String

Parameters

value

An array of bytes.

Return Value

A String of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A".

Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

All the elements of value are converted.

The following code example converts Byte arrays to String objects with the ToString method.

// Example of the BitConverter.ToString( byte[ ] ) method.
using System;

class BytesToStringDemo
{
    // Display a byte array with a name.
    public static void WriteByteArray( byte[ ] bytes, string name )
    {
        const string underLine = "--------------------------------";

        Console.WriteLine( name );
        Console.WriteLine( underLine.Substring( 0, 
            Math.Min( name.Length, underLine.Length ) ) );
        Console.WriteLine( BitConverter.ToString( bytes ) );
        Console.WriteLine( );
    }

    public static void Main( )
    {
        byte[ ] arrayOne = {
             0,   1,   2,   4,   8,  16,  32,  64, 128, 255 };

        byte[ ] arrayTwo = {
            32,   0,   0,  42,   0,  65,   0, 125,   0, 197,
             0, 168,   3,  41,   4, 172,  32 };

        byte[ ] arrayThree = {
            15,   0,   0, 128,  16,  39, 240, 216, 241, 255, 
           127 };

        byte[ ] arrayFour = {
            15,   0,   0,   0,   0,  16,   0, 255,   3,   0, 
             0, 202, 154,  59, 255, 255, 255, 255, 127 };

        Console.WriteLine( "This example of the " +
            "BitConverter.ToString( byte[ ] ) \n" +
            "method generates the following output.\n" );

        WriteByteArray( arrayOne, "arrayOne" );
        WriteByteArray( arrayTwo, "arrayTwo" );
        WriteByteArray( arrayThree, "arrayThree" );
        WriteByteArray( arrayFour, "arrayFour" );
    }
}

/*
This example of the BitConverter.ToString( byte[ ] )
method generates the following output.

arrayOne
--------
00-01-02-04-08-10-20-40-80-FF

arrayTwo
--------
20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-AC-20

arrayThree
----------
0F-00-00-80-10-27-F0-D8-F1-FF-7F

arrayFour
---------
0F-00-00-00-00-10-00-FF-03-00-00-CA-9A-3B-FF-FF-FF-FF-7F
*/

// Example of the BitConverter.ToString( byte[ ] ) method.
import System.*;

class BytesToStringDemo
{
    // Display a byte array with a name.
    public static void WriteByteArray(ubyte bytes[], String name)
    {
        final String underLine = "--------------------------------";

        Console.WriteLine(name);
        Console.WriteLine(underLine.Substring(0, System.Math.Min(name.length(),
            underLine.length())));
        Console.WriteLine(BitConverter.ToString(bytes));
        Console.WriteLine();
    } //WriteByteArray

    public static void main(String[] args)
    {
        ubyte arrayOne[] =  { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 };
        ubyte arrayTwo[] = { 32, 0, 0, 42, 0, 65, 0, 125, 0, 197, 0, 168, 3, 
            41, 4, 172, 32};
        ubyte arrayThree[] =  { 15, 0, 0, 128, 16, 39, 240, 216, 241, 255, 127 };
        ubyte arrayFour[] = { 15, 0, 0, 0, 0, 16, 0, 255, 3, 0, 0, 202, 154, 
            59, 255, 255, 255, 255, 127};

        Console.WriteLine(("This example of the "
            + "BitConverter.ToString( byte[ ] ) \n"
            + "method generates the following output.\n"));
        WriteByteArray(arrayOne, "arrayOne");
        WriteByteArray(arrayTwo, "arrayTwo");
        WriteByteArray(arrayThree, "arrayThree");
        WriteByteArray(arrayFour, "arrayFour");
    } //main
} //BytesToStringDemo

/*
This example of the BitConverter.ToString( byte[ ] )
method generates the following output.

arrayOne
--------
00-01-02-04-08-10-20-40-80-FF

arrayTwo
--------
20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-AC-20

arrayThree
----------
0F-00-00-80-10-27-F0-D8-F1-FF-7F

arrayFour
---------
0F-00-00-00-00-10-00-FF-03-00-00-CA-9A-3B-FF-FF-FF-FF-7F
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: