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)

public static string ToString(
	byte[] value
)

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

ExceptionCondition
ArgumentNullException

value is null.

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 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
*/


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

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

Community Additions

ADD
Show: