将值复制到安全上下文中特定偏移量的字节数组中
Copying values into a byte array at a specific offset in a safe context
我正在尝试将 uint 的值复制到 C# 中的字节数组中。我已经设法在不安全的上下文中使用代码来完成此操作,但理想情况下,我想在安全的上下文中执行此操作
我目前使用的代码是这样的
var bytes = new byte[] {0x68, 0x00, 0x00, 0x00, 0x00}
fixed (byte* bytesPointer = bytes )
{
*(ulong*)(bytesPointer + 1) = value;
}
我在 C# 中尝试完成的等效项在 C++ 中可以像这样完成
unsigned char bytes[] = {0x68, 0x00, 0x00, 0x00, 0x00}
memcpy(((unsigned long)bytes + 1), value, 4);
如何在 C# 中的安全上下文中执行此操作?
你可以使用这些
Array.Copy(Array, Int32, Array, Int32, Int32)
Copies a range of elements from an Array starting at the specified
source index and pastes them to another Array starting at the
specified destination index. The length and the indexes are specified
as 32-bit integers.
Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method
Copies a specified number of bytes from a source array starting at a
particular offset to a destination array starting at a particular
offset.
我正在尝试将 uint 的值复制到 C# 中的字节数组中。我已经设法在不安全的上下文中使用代码来完成此操作,但理想情况下,我想在安全的上下文中执行此操作
我目前使用的代码是这样的
var bytes = new byte[] {0x68, 0x00, 0x00, 0x00, 0x00}
fixed (byte* bytesPointer = bytes )
{
*(ulong*)(bytesPointer + 1) = value;
}
我在 C# 中尝试完成的等效项在 C++ 中可以像这样完成
unsigned char bytes[] = {0x68, 0x00, 0x00, 0x00, 0x00}
memcpy(((unsigned long)bytes + 1), value, 4);
如何在 C# 中的安全上下文中执行此操作?
你可以使用这些
Array.Copy(Array, Int32, Array, Int32, Int32)
Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 32-bit integers.
Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method
Copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.