Marshal.ReadByte Method

Definition

Lee un único byte desde la memoria no administrada.Reads a single byte from unmanaged memory. Se admite la lectura desde ubicaciones de memoria desalineadas.Reading from unaligned memory locations is supported.

Overloads

ReadByte(IntPtr, Int32)

Lee un único byte en un desplazamiento (o índice) dado desde la memoria no administrada.Reads a single byte at a given offset (or index) from unmanaged memory.

ReadByte(Object, Int32)

Lee un único byte en un desplazamiento (o índice) dado desde la memoria no administrada.Reads a single byte at a given offset (or index) from unmanaged memory.

ReadByte(IntPtr)

Lee un único byte desde la memoria no administrada.Reads a single byte from unmanaged memory.

ReadByte(IntPtr, Int32)

Lee un único byte en un desplazamiento (o índice) dado desde la memoria no administrada.Reads a single byte at a given offset (or index) from unmanaged memory.

public:
 static System::Byte ReadByte(IntPtr ptr, int ofs);
[System.Security.SecurityCritical]
public static byte ReadByte (IntPtr ptr, int ofs);
public static byte ReadByte (IntPtr ptr, int ofs);
static member ReadByte : nativeint * int -> byte
Public Shared Function ReadByte (ptr As IntPtr, ofs As Integer) As Byte

Parameters

ptr
IntPtr

Dirección base en la memoria no administrada desde la que se va a leer.The base address in unmanaged memory from which to read.

ofs
Int32

Desplazamiento de bytes adicional, que se agrega al parámetro ptr antes de la lectura.An additional byte offset, which is added to the ptr parameter before reading.

Returns

Byte

Byte leído de la memoria no administrada en el desplazamiento dado.The byte read from unmanaged memory at the given offset.

Attributes

Exceptions

La dirección base (ptr) más el byte de desplazamiento (ofs) produce un valor null o una dirección no válida.Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

Examples

En el ejemplo siguiente se muestra cómo leer y escribir en una matriz no administrada mediante los métodos ReadByte y WriteByte.The following example demonstrates how to read and write to an unmanaged array using the ReadByte and WriteByte methods.

static void ReadWriteByte()
{
    // Allocate unmanaged memory. 
    int elementSize = 1;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteByte(unmanagedArray, i * elementSize, ((Byte)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadByte(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteByte()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 1
    Dim unmanagedArray As IntPtr = Marshal.AllocHGlobal(10 * elementSize)

    ' Set the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Marshal.WriteByte(unmanagedArray, i * elementSize, CType(i + 1, Byte))
    Next i
    Console.WriteLine("Unmanaged memory written.")

    Console.WriteLine("Reading unmanaged memory:")
    ' Print the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Console.WriteLine(Marshal.ReadByte(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

En el ejemplo siguiente se muestra cómo utilizar el método ReadByte para leer el valor de un carácter no administrado.The following example demonstrates how to use the ReadByte method to read the value of an unmanaged character.


using namespace System;
using namespace System::Runtime::InteropServices;



void main()
{
    // Create an unmanaged byte.
    const char * myString = "bB";

    // Read the second character of the c string as a managed byte.
        Byte ^ myManagedByte = Marshal::ReadByte((IntPtr) (char *) myString, 1);

    // Display the byte to the console.
    Console::WriteLine(myManagedByte);
}

Remarks

ReadByte habilita la interacción directa con una matriz de bytes de estilo C no administrada, lo que elimina el gasto de copiar una matriz no administrada completa (mediante Marshal.Copy) en una matriz administrada independiente antes de leer sus valores de elemento.ReadByte enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Se admite la lectura desde ubicaciones de memoria desalineadas.Reading from unaligned memory locations is supported.

See also

ReadByte(Object, Int32)

Warning

This API is now obsolete.

Lee un único byte en un desplazamiento (o índice) dado desde la memoria no administrada.Reads a single byte at a given offset (or index) from unmanaged memory.

public:
 static System::Byte ReadByte(System::Object ^ ptr, int ofs);
[System.Obsolete("ReadByte(Object, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static byte ReadByte (object ptr, int ofs);
[System.Obsolete("ReadByte(Object, Int32) may be unavailable in future releases.")]
public static byte ReadByte (object ptr, int ofs);
public static byte ReadByte (object ptr, int ofs);
[System.Security.SecurityCritical]
public static byte ReadByte (object ptr, int ofs);
static member ReadByte : obj * int -> byte
Public Shared Function ReadByte (ptr As Object, ofs As Integer) As Byte

Parameters

ptr
Object

Dirección base en la memoria no administrada del objeto de origen.The base address in unmanaged memory of the source object.

ofs
Int32

Desplazamiento de bytes adicional, que se agrega al parámetro ptr antes de la lectura.An additional byte offset, which is added to the ptr parameter before reading.

Returns

Byte

Byte leído de la memoria no administrada en el desplazamiento dado.The byte read from unmanaged memory at the given offset.

Attributes

Exceptions

La dirección base (ptr) más el byte de desplazamiento (ofs) produce un valor null o una dirección no válida.Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

ptr es un objeto ArrayWithOffset.ptr is an ArrayWithOffset object. Este método no acepta parámetros ArrayWithOffset.This method does not accept ArrayWithOffset parameters.

Remarks

ReadByte habilita la interacción directa con una matriz de bytes de estilo C no administrada, lo que elimina el gasto de copiar una matriz no administrada completa (mediante Marshal.Copy) en una matriz administrada independiente antes de leer sus valores de elemento.ReadByte enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Se admite la lectura desde ubicaciones de memoria desalineadas.Reading from unaligned memory locations is supported.

See also

ReadByte(IntPtr)

Lee un único byte desde la memoria no administrada.Reads a single byte from unmanaged memory.

public:
 static System::Byte ReadByte(IntPtr ptr);
[System.Security.SecurityCritical]
public static byte ReadByte (IntPtr ptr);
public static byte ReadByte (IntPtr ptr);
static member ReadByte : nativeint -> byte
Public Shared Function ReadByte (ptr As IntPtr) As Byte

Parameters

ptr
IntPtr

Dirección en la memoria no administrada desde la que se va a leer.The address in unmanaged memory from which to read.

Returns

Byte

Byte leído de la memoria no administrada.The byte read from unmanaged memory.

Attributes

Exceptions

ptr no es un formato reconocido.ptr is not a recognized format.

O bien-or- ptr es null.ptr is null.

O bien-or- ptr no es válido.ptr is invalid.

Examples

En el ejemplo siguiente se crea un bloque de memoria no administrada, se escribe un byte en la memoria no administrada, se lee el byte de la memoria no administrada y, a continuación, se desecha la memoria no administrada.The following example creates a block of unmanaged memory, writes a byte to the unmanaged memory, reads the byte back from unmanaged memory, and then disposes the unmanaged memory.

using System;
using System.Runtime.InteropServices;

 class Example
 {
     static void Main(string[] args)
     {
          // Allocate 1 byte of unmanaged memory.
          IntPtr hGlobal = Marshal.AllocHGlobal(1);

          // Create a new byte.
          byte b = 1;
          Console.WriteLine("Byte written to unmanaged memory: " + b);

          // Write the byte to unmanaged memory.
          Marshal.WriteByte(hGlobal, b);

          // Read byte from unmanaged memory.
          byte c = Marshal.ReadByte(hGlobal);
          Console.WriteLine("Byte read from unmanaged memory: " + c);

          // Free the unmanaged memory.
          Marshal.FreeHGlobal(hGlobal);
          Console.WriteLine("Unmanaged memory was disposed.");
     }
}
Imports System.Runtime.InteropServices

Module Example
    Sub Main()
         ' Allocate 1 byte of unmanaged memory.
         Dim hGlobal As IntPtr = Marshal.AllocHGlobal(1)
         
         ' Create a new byte.
         Dim b As Byte = 1
         
         Console.WriteLine("Byte written to unmanaged memory: {0}", b)
         
         ' Write the byte to unmanaged memory.
         Marshal.WriteByte(hGlobal, b)
         
         ' Read byte from unmanaged memory.
         Dim c As Byte = Marshal.ReadByte(hGlobal)
         Console.WriteLine("Byte read from unmanaged memory: {0}", c)
         
         ' Free the unmanaged memory.
         Marshal.FreeHGlobal(hGlobal)
         Console.WriteLine("Unmanaged memory was disposed.")
    End Sub
End Module

En el ejemplo siguiente se muestra cómo utilizar el método ReadByte para leer el valor de un carácter no administrado.The following example demonstrates how to use the ReadByte method to read the value of an unmanaged character.


using namespace System;
using namespace System::Runtime::InteropServices;



void main()
{
    // Create an unmanaged byte.
    const char * myString = "b";

    // Read the c string as a managed byte.
        Byte ^ myManagedByte = Marshal::ReadByte((IntPtr) (char *) myString);

    // Display the byte to the console.
    Console::WriteLine(myManagedByte);
}

Remarks

ReadByte tiene un desplazamiento implícito de 0.ReadByte has an implied offset of 0. Este método permite la interacción directa con una matriz de bytes de estilo C no administrada, lo que elimina el gasto de copiar una matriz no administrada completa (mediante Marshal.Copy) en una matriz administrada independiente antes de leer sus valores de elemento.This method enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Se admite la lectura desde ubicaciones de memoria desalineadas.Reading from unaligned memory locations is supported.

See also

Applies to