Ler en inglés

Compartir por


Buffer.GetByte(Array, Int32) Método

Definición

Recupera el byte en la posición especificada de la matriz determinada.

C#
public static byte GetByte (Array array, int index);

Parámetros

array
Array

Matriz .

index
Int32

Una posición en la matriz.

Devoluciones

Byte

Byte en la posición especificada de la matriz.

Excepciones

array no es una matriz primitiva.

array es null.

index es negativo o mayor que la longitud de array.

array es mayor de 2 gigabytes (GB).

Ejemplos

En el ejemplo de código siguiente se muestran los valores de bytes en ubicaciones especificadas dentro de matrices mediante el GetByte método .

C#
// Example of the Buffer.GetByte method.
using System;

class GetByteDemo
{
    const string formatter = "{0,10}{1,10}{2,9} {3}";

    // Display the array contents in hexadecimal.
    public static void DisplayArray( Array arr, string name )
    {
        // Get the array element width; format the formatting string.
        int elemWidth = Buffer.ByteLength( arr ) / arr.Length;
        string format = String.Format( " {{0:X{0}}}", 2 * elemWidth );

        // Display the array elements from right to left.
        Console.Write( "{0,5}:", name );
        for( int loopX = arr.Length - 1; loopX >= 0; loopX-- )
            Console.Write( format, arr.GetValue( loopX ) );
        Console.WriteLine( );
    }

    public static void ArrayInfo( Array arr, string name, int index )
    {
        byte value = Buffer.GetByte( arr, index );

        // Display the array name, index, and byte to be viewed.
        Console.WriteLine( formatter, name, index, value,
            String.Format( "0x{0:X2}", value ) );
    }

    public static void Main( )
    {
        // These are the arrays to be viewed with GetByte.
        long[ ] longs =
            { 333333333333333333, 666666666666666666, 999999999999999999 };
        int[ ]  ints  =
            { 111111111, 222222222, 333333333, 444444444, 555555555 };

        Console.WriteLine( "This example of the " +
            "Buffer.GetByte( Array, int ) \n" +
            "method generates the following output.\n" +
            "Note: The arrays are displayed from right to left.\n" );
        Console.WriteLine( "  Values of arrays:\n" );

        // Display the values of the arrays.
        DisplayArray( longs, "longs" );
        DisplayArray( ints, "ints" );
        Console.WriteLine( );

        Console.WriteLine( formatter, "Array", "index", "value", "" );
        Console.WriteLine( formatter, "-----", "-----", "-----",
            "----" );

        // Display the Length and ByteLength for each array.
        ArrayInfo( ints, "ints", 0 );
        ArrayInfo( ints, "ints", 7 );
        ArrayInfo( ints, "ints", 10 );
        ArrayInfo( ints, "ints", 17 );
        ArrayInfo( longs, "longs", 0 );
        ArrayInfo( longs, "longs", 6 );
        ArrayInfo( longs, "longs", 10 );
        ArrayInfo( longs, "longs", 17 );
        ArrayInfo( longs, "longs", 21 );
    }
}

/*
This example of the Buffer.GetByte( Array, int )
method generates the following output.
Note: The arrays are displayed from right to left.

  Values of arrays:

longs: 0DE0B6B3A763FFFF 094079CD1A42AAAA 04A03CE68D215555
 ints: 211D1AE3 1A7DAF1C 13DE4355 0D3ED78E 069F6BC7

     Array     index    value
     -----     -----    ----- ----
      ints         0      199 0xC7
      ints         7       13 0x0D
      ints        10      222 0xDE
      ints        17       26 0x1A
     longs         0       85 0x55
     longs         6      160 0xA0
     longs        10       66 0x42
     longs        17      255 0xFF
     longs        21      182 0xB6
*/

Comentarios

El GetByte método obtiene un byte determinado de la matriz. La matriz debe ser una matriz de primitivos.

Se aplica a

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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0