Export (0) Print
Expand All

BitConverter.ToString Method (Byte())

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)

'Declaration
Public Shared Function ToString ( _
	value As Byte() _
) As String

Parameters

value
Type: System.Byte()

An array of bytes.

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-00".

ExceptionCondition
ArgumentNullException

value is Nothing.

All the elements of value are converted. The order of hexadecimal strings returned by the ToString method depends on whether the computer architecture is little-endian or big-endian.

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

' Example of the BitConverter.ToString( Byte( ) ) method. 
Imports System
Imports Microsoft.VisualBasic

Module BytesToStringDemo

    ' Display a Byte array with a name. 
    Sub WriteByteArray( bytes( ) As Byte, name As String )

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

        Console.WriteLine( name )
        Console.WriteLine( underLine.Substring( 0, _
            Math.Min( name.Length, underLine.Length ) ) )
        Console.WriteLine( BitConverter.ToString( bytes ) )
        Console.WriteLine( )
    End Sub 

    Sub Main( )
        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 }

        Console.WriteLine( "This example of the " & _
            "BitConverter.ToString( Byte( ) ) " & vbCrLf & _
            "method generates the following output." & vbCrLf )

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft