Freigeben über


Buffer.GetByte-Methode

Ruft das Byte an einer angegebenen Position in einem angegebenen Array ab.

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

Syntax

'Declaration
Public Shared Function GetByte ( _
    array As Array, _
    index As Integer _
) As Byte
'Usage
Dim array As Array
Dim index As Integer
Dim returnValue As Byte

returnValue = Buffer.GetByte(array, index)
public static byte GetByte (
    Array array,
    int index
)
public:
static unsigned char GetByte (
    Array^ array, 
    int index
)
public static byte GetByte (
    Array array, 
    int index
)
public static function GetByte (
    array : Array, 
    index : int
) : byte

Parameter

  • array
    Ein Array.
  • index
    Eine Position im Array.

Rückgabewert

Gibt das index-Byte im Array zurück.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentException

array ist kein Array von Primitiven.

ArgumentNullException

array ist NULL (Nothing in Visual Basic).

ArgumentOutOfRangeException

index ist negativ oder größer als die Länge von array.

Hinweise

Die GetByte-Methode ruft ein bestimmtes Byte aus dem Array ab. Das Array muss ein Array von Primitiven sein.

Beispiel

Im folgenden Codebeispiel werden die Bytewerte an den angegebenen Positionen in Arrays mithilfe der GetByte-Methode angezeigt.

' Example of the Buffer.GetByte method.
Imports System
Imports Microsoft.VisualBasic

Module GetByteDemo

    Const formatter As String = "{0,10}{1,10}{2,9} {3}"

    ' Display the array contents in hexadecimal.
    Sub DisplayArray( arr As Array, name As String )

        ' Get the array element width; format the formatting string.
        Dim loopX     As Integer
        Dim elemWidth As Integer = _
            Buffer.ByteLength( arr ) / arr.Length
        Dim format    As String = _
            String.Format( " {{0:X{0}}}", 2 * elemWidth )

        ' Display the array elements from right to left.
        Console.Write( "{0,7}:", name )
        For loopX = arr.Length - 1 to 0 Step -1
            Console.Write( format, arr( loopX ) )
        Next loopX
        Console.WriteLine( )
    End Sub

    Sub ArrayInfo( arr As Array, name As String, index As Integer )
        
        Dim value As Byte = Buffer.GetByte( arr, index )

        ' Display the array name, index, and byte to be viewed.
        Console.WriteLine( formatter, name, index, value, _
            String.Format( "&H{0:X2}", value ) )
    End Sub

    Sub Main( )

        ' These are the arrays to be viewed with GetByte.
        Dim longs( ) As Long  = _
            { 333333333333333333, 666666666666666666, 999999999999999999 }
        Dim ints( )  As Integer = _
            { 111111111, 222222222, 333333333, 444444444, 555555555 }

        Console.WriteLine( "This example of the " & _
            "Buffer.GetByte( Array, Integer ) " & vbCrLf & _
            "method generates the following output." & vbCrLf & _
            "Note: The arrays are displayed from right to left." )
        Console.WriteLine( vbCrLf & "  Values of arrays:" & vbCrLf )

        ' 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 )
    End Sub 
End Module 

' This example of the Buffer.GetByte( Array, Integer )
' 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 &HC7
'       ints         7       13 &H0D
'       ints        10      222 &HDE
'       ints        17       26 &H1A
'      longs         0       85 &H55
'      longs         6      160 &HA0
'      longs        10       66 &H42
'      longs        17      255 &HFF
'      longs        21      182 &HB6
// 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
*/
// Example of the Buffer::GetByte method.
using namespace System;
#define formatter "{0,10}{1,10}{2,9} {3}"

// Display the array contents in hexadecimal.
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();
}

void ArrayInfo( Array^ arr, String^ name, int index )
{
   unsigned char 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 ) );
}

int main()
{
   
   // These are the arrays to be viewed with GetByte.
   array<__int64>^longs = {333333333333333333,666666666666666666,999999999999999999};
   array<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
*/
// Example of the Buffer.GetByte method.
import System.*;

class GetByteDemo
{
    private static 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.get_Length();
        String format = String.Format(" {{0:X{0}}}", (Int32)(2 * elemWidth));

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

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

        // Display the array name, index, and byte to be viewed.
        Object objArr[] = new Object[] { name, (Int32)index, (UInt16)value, 
            String.Format("0x{0:X2}", (UInt16)value) };
        Console.WriteLine(formatter, objArr);
    } //ArrayInfo

    public static void main(String[] args)
    {
        // These are the arrays to be viewed with GetByte.
        long longs[] =  { 333333333333333333L, 666666666666666666L, 
                            999999999999999999L };
        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();

        Object objArr1[] = new Object[] { "Array", "index", "value", ""};
        Console.WriteLine(formatter, objArr1);

        Object objArr2[] = new Object[] { "-----", "-----","-----", "----" };
        Console.WriteLine(formatter, objArr2);

        // 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);
    } //main
} //GetByteDemo

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

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Buffer-Klasse
Buffer-Member
System-Namespace