Ler en inglés

Compartir por


BitConverter.ToString Método

Definición

Convierte el valor numérico de cada elemento de una matriz especificada de bytes en su representación de cadena hexadecimal equivalente.

Sobrecargas

ToString(Byte[])

Convierte el valor numérico de cada elemento de una matriz especificada de bytes en su representación de cadena hexadecimal equivalente.

ToString(Byte[], Int32)

Convierte el valor numérico de cada elemento de una submatriz especificada de bytes en su representación de cadena hexadecimal equivalente.

ToString(Byte[], Int32, Int32)

Convierte el valor numérico de cada elemento de una submatriz especificada de bytes en su representación de cadena hexadecimal equivalente.

ToString(Byte[])

Source:
BitConverter.cs
Source:
BitConverter.cs
Source:
BitConverter.cs

Convierte el valor numérico de cada elemento de una matriz especificada de bytes en su representación de cadena hexadecimal equivalente.

C#
public static string ToString (byte[] value);

Parámetros

value
Byte[]

Matriz de bytes.

Devoluciones

Cadena de pares hexadecimales separados por guiones, donde cada par representa el elemento correspondiente en value; por ejemplo, "7F-2C-4A-00".

Excepciones

value es null.

Ejemplos

En el ejemplo de código siguiente se convierten matrices Byte en String objetos con el ToString método .

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

Comentarios

Todos los elementos de value se convierten.

Se aplica a

.NET 9 e outras versións
Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(Byte[], Int32)

Source:
BitConverter.cs
Source:
BitConverter.cs
Source:
BitConverter.cs

Convierte el valor numérico de cada elemento de una submatriz especificada de bytes en su representación de cadena hexadecimal equivalente.

C#
public static string ToString (byte[] value, int startIndex);

Parámetros

value
Byte[]

Matriz de bytes.

startIndex
Int32

Posición inicial de value.

Devoluciones

Cadena de pares hexadecimales separados por guiones, donde cada par representa el elemento correspondiente en una submatriz de value; por ejemplo, "7F-2C-4A-00".

Excepciones

value es null.

startIndex es menor que cero o mayor que la longitud de value menos 1.

Ejemplos

En el ejemplo de código siguiente se convierte la parte de una Byte matriz que comienza en el especificado startIndex en un String con el ToString método .

C#
// Example of some BitConverter.ToString( ) method overloads.
using System;

class BytesToStringDemo
{
    // Display a byte array, using multiple lines if necessary.
    public static void WriteMultiLineByteArray( byte[ ] bytes,
        string name )
    {
        const int rowSize = 20;
        const string underLine = "--------------------------------";
        int iter;

        Console.WriteLine( name );
        Console.WriteLine( underLine.Substring( 0,
            Math.Min( name.Length, underLine.Length ) ) );

        for( iter = 0; iter < bytes.Length - rowSize; iter += rowSize )
        {
            Console.Write(
                BitConverter.ToString( bytes, iter, rowSize ) );
            Console.WriteLine( "-" );
        }

        Console.WriteLine( BitConverter.ToString( bytes, iter ) );
        Console.WriteLine( );
    }

    public static void Main( )
    {
        byte[ ] arrayOne = {
              0,   0,   0,   0, 128,  63,   0,   0, 112,  65,
              0, 255, 127,  71,   0,   0, 128,  59,   0,   0,
            128,  47,  73,  70, 131,   5,  75,   6, 158,  63,
             77,   6, 158,  63,  80,   6, 158,  63,  30,  55,
            190, 121, 255, 255, 127, 255, 255, 127, 127,   1,
              0,   0,   0, 192, 255,   0,   0, 128, 255,   0,
              0, 128, 127 };

        byte[ ] arrayTwo = {
            255, 255, 255,   0,   0,  20,   0,  33,   0,   0,
              0,   1,   0,   0,   0, 100, 167, 179, 182, 224,
             13,   0, 202, 154,  59,   0, 143,  91,   0, 170,
            170, 170, 170, 170, 170,   0,   0, 232, 137,   4,
             35, 199, 138, 255, 232, 244, 255, 252, 205, 255,
            255, 129 };

        byte[ ] arrayThree = {
              0, 222,   0,   0,   0, 224, 111,  64,   0,   0,
            224, 255, 255, 255, 239,  65,   0,   0, 131,   0,
              0,   0, 112,  63,   0, 143,   0, 100,   0,   0,
            240,  61, 223, 136,  30,  28, 254, 116, 170,   1,
            250,  89, 140,  66, 202, 192, 243,  63, 251,  89,
            140,  66, 202, 192, 243,  63, 252,  89, 140,  66,
            202, 192, 243,  63,  82, 211, 187, 188, 232, 126,
            255, 255, 255, 244, 255, 239, 127,   1,   0,   0,
              0,  10,  17,   0,   0, 248, 255,   0,  88,   0,
             91,   0,   0, 240, 255,   0,   0, 240, 157 };

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

        WriteMultiLineByteArray( arrayOne, "arrayOne" );
        WriteMultiLineByteArray( arrayTwo, "arrayTwo" );
        WriteMultiLineByteArray( arrayThree, "arrayThree" );
    }
}

/*
This example of the
  BitConverter.ToString( byte[ ], int ) and
  BitConverter.ToString( byte[ ], int, int )
methods generates the following output.

arrayOne
--------
00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
BE-79-FF-FF-7F-FF-FF-7F-7F-01-00-00-00-C0-FF-00-00-80-FF-00-
00-80-7F

arrayTwo
--------
FF-FF-FF-00-00-14-00-21-00-00-00-01-00-00-00-64-A7-B3-B6-E0-
0D-00-CA-9A-3B-00-8F-5B-00-AA-AA-AA-AA-AA-AA-00-00-E8-89-04-
23-C7-8A-FF-E8-F4-FF-FC-CD-FF-FF-81

arrayThree
----------
00-DE-00-00-00-E0-6F-40-00-00-E0-FF-FF-FF-EF-41-00-00-83-00-
00-00-70-3F-00-8F-00-64-00-00-F0-3D-DF-88-1E-1C-FE-74-AA-01-
FA-59-8C-42-CA-C0-F3-3F-FB-59-8C-42-CA-C0-F3-3F-FC-59-8C-42-
CA-C0-F3-3F-52-D3-BB-BC-E8-7E-FF-FF-FF-F4-FF-EF-7F-01-00-00-
00-0A-11-00-00-F8-FF-00-58-00-5B-00-00-F0-FF-00-00-F0-9D
*/

Comentarios

Los elementos de la posición startIndex de la matriz hasta el final de la matriz se convierten.

Se aplica a

.NET 9 e outras versións
Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(Byte[], Int32, Int32)

Source:
BitConverter.cs
Source:
BitConverter.cs
Source:
BitConverter.cs

Convierte el valor numérico de cada elemento de una submatriz especificada de bytes en su representación de cadena hexadecimal equivalente.

C#
public static string ToString (byte[] value, int startIndex, int length);

Parámetros

value
Byte[]

Matriz de bytes que incluye los bytes que se van a convertir.

startIndex
Int32

Posición inicial de value.

length
Int32

Número de elementos de matriz de value que se van a convertir.

Devoluciones

Cadena de pares hexadecimales separados por guiones, donde cada par representa el elemento correspondiente en una submatriz de value; por ejemplo, "7F-2C-4A-00".

Excepciones

value es null.

startIndex o length es menor que cero.

o bien

startIndex es mayor que cero y es mayor o igual que la longitud de value.

La combinación de startIndex y length no especifica ninguna posición dentro de value; es decir, el parámetro startIndex es mayor que la longitud de value menos el parámetro length.

Ejemplos

En el ejemplo siguiente se usa el ToString método para convertir parte de una matriz de bytes, empezando por el especificado startIndex y con el especificado length, en una cadena.

C#
// Example of some BitConverter.ToString( ) method overloads.
using System;

class BytesToStringDemo
{
    // Display a byte array, using multiple lines if necessary.
    public static void WriteMultiLineByteArray( byte[ ] bytes,
        string name )
    {
        const int rowSize = 20;
        const string underLine = "--------------------------------";
        int iter;

        Console.WriteLine( name );
        Console.WriteLine( underLine.Substring( 0,
            Math.Min( name.Length, underLine.Length ) ) );

        for( iter = 0; iter < bytes.Length - rowSize; iter += rowSize )
        {
            Console.Write(
                BitConverter.ToString( bytes, iter, rowSize ) );
            Console.WriteLine( "-" );
        }

        Console.WriteLine( BitConverter.ToString( bytes, iter ) );
        Console.WriteLine( );
    }

    public static void Main( )
    {
        byte[ ] arrayOne = {
              0,   0,   0,   0, 128,  63,   0,   0, 112,  65,
              0, 255, 127,  71,   0,   0, 128,  59,   0,   0,
            128,  47,  73,  70, 131,   5,  75,   6, 158,  63,
             77,   6, 158,  63,  80,   6, 158,  63,  30,  55,
            190, 121, 255, 255, 127, 255, 255, 127, 127,   1,
              0,   0,   0, 192, 255,   0,   0, 128, 255,   0,
              0, 128, 127 };

        byte[ ] arrayTwo = {
            255, 255, 255,   0,   0,  20,   0,  33,   0,   0,
              0,   1,   0,   0,   0, 100, 167, 179, 182, 224,
             13,   0, 202, 154,  59,   0, 143,  91,   0, 170,
            170, 170, 170, 170, 170,   0,   0, 232, 137,   4,
             35, 199, 138, 255, 232, 244, 255, 252, 205, 255,
            255, 129 };

        byte[ ] arrayThree = {
              0, 222,   0,   0,   0, 224, 111,  64,   0,   0,
            224, 255, 255, 255, 239,  65,   0,   0, 131,   0,
              0,   0, 112,  63,   0, 143,   0, 100,   0,   0,
            240,  61, 223, 136,  30,  28, 254, 116, 170,   1,
            250,  89, 140,  66, 202, 192, 243,  63, 251,  89,
            140,  66, 202, 192, 243,  63, 252,  89, 140,  66,
            202, 192, 243,  63,  82, 211, 187, 188, 232, 126,
            255, 255, 255, 244, 255, 239, 127,   1,   0,   0,
              0,  10,  17,   0,   0, 248, 255,   0,  88,   0,
             91,   0,   0, 240, 255,   0,   0, 240, 157 };

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

        WriteMultiLineByteArray( arrayOne, "arrayOne" );
        WriteMultiLineByteArray( arrayTwo, "arrayTwo" );
        WriteMultiLineByteArray( arrayThree, "arrayThree" );
    }
}

/*
This example of the
  BitConverter.ToString( byte[ ], int ) and
  BitConverter.ToString( byte[ ], int, int )
methods generates the following output.

arrayOne
--------
00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
BE-79-FF-FF-7F-FF-FF-7F-7F-01-00-00-00-C0-FF-00-00-80-FF-00-
00-80-7F

arrayTwo
--------
FF-FF-FF-00-00-14-00-21-00-00-00-01-00-00-00-64-A7-B3-B6-E0-
0D-00-CA-9A-3B-00-8F-5B-00-AA-AA-AA-AA-AA-AA-00-00-E8-89-04-
23-C7-8A-FF-E8-F4-FF-FC-CD-FF-FF-81

arrayThree
----------
00-DE-00-00-00-E0-6F-40-00-00-E0-FF-FF-FF-EF-41-00-00-83-00-
00-00-70-3F-00-8F-00-64-00-00-F0-3D-DF-88-1E-1C-FE-74-AA-01-
FA-59-8C-42-CA-C0-F3-3F-FB-59-8C-42-CA-C0-F3-3F-FC-59-8C-42-
CA-C0-F3-3F-52-D3-BB-BC-E8-7E-FF-FF-FF-F4-FF-EF-7F-01-00-00-
00-0A-11-00-00-F8-FF-00-58-00-5B-00-00-F0-FF-00-00-F0-9D
*/

Comentarios

Los length elementos de la posición startIndex de la matriz se convierten. Si length es igual a cero, el método devuelve String.Empty.

Se aplica a

.NET 9 e outras versións
Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0