BitConverter.ToString Method (array<Byte[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation.

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

Syntax

'Declaration
Public Shared Function ToString ( _
    value As Byte() _
) As String
public static string ToString(
    byte[] value
)

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

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

Remarks

All the elements of value are converted.

Examples

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

' Example of the BitConverter.ToString( Byte( ) ) method.

Module Example

   ' Display a Byte array with a name.
   Sub WriteByteArray(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal bytes() As Byte, ByVal name As String)

      Const underLine As String = "--------------------------------"

      outputBlock.Text &= name & vbCrLf
      outputBlock.Text &= String.Format(underLine.Substring(0, _
          Math.Min(name.Length, underLine.Length))) & vbCrLf
      outputBlock.Text &= BitConverter.ToString(bytes) & vbCrLf
      outputBlock.Text &= vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim arrayOne As Byte() = { _
            0, 1, 2, 4, 8, 16, 32, 64, 128, 255}

      Dim arrayTwo As Byte() = { _
           32, 0, 0, 42, 0, 65, 0, 125, 0, 197, _
            0, 168, 3, 41, 4, 172, 32}

      Dim arrayThree As Byte() = { _
           15, 0, 0, 128, 16, 39, 240, 216, 241, 255, _
          127}

      Dim arrayFour As Byte() = { _
           15, 0, 0, 0, 0, 16, 0, 255, 3, 0, _
            0, 202, 154, 59, 255, 255, 255, 255, 127}

      outputBlock.Text &= String.Format("This example of the " & _
          "BitConverter.ToString( Byte( ) ) " & vbCrLf & _
          "method generates the following output." & vbCrLf) & vbCrLf

      WriteByteArray(outputBlock, arrayOne, "arrayOne")
      WriteByteArray(outputBlock, arrayTwo, "arrayTwo")
      WriteByteArray(outputBlock, arrayThree, "arrayThree")
      WriteByteArray(outputBlock, arrayFour, "arrayFour")
   End Sub
End Module

' 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.
using System;

class Example
{
   // Display a byte array with a name.
   public static void WriteByteArray(System.Windows.Controls.TextBlock outputBlock, byte[] bytes, string name)
   {
      const string underLine = "--------------------------------";

      outputBlock.Text += name + "\n";
      outputBlock.Text += String.Format(underLine.Substring(0,
          Math.Min(name.Length, underLine.Length))) + "\n";
      outputBlock.Text += BitConverter.ToString(bytes) + "\n";
      outputBlock.Text += "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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 };

      outputBlock.Text += String.Format("This example of the " +
          "BitConverter.ToString( byte[ ] ) \n" +
          "method generates the following output.\n") + "\n";

      WriteByteArray(outputBlock, arrayOne, "arrayOne");
      WriteByteArray(outputBlock, arrayTwo, "arrayTwo");
      WriteByteArray(outputBlock, arrayThree, "arrayThree");
      WriteByteArray(outputBlock, 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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.