简体   繁体   English

对于int,memcpy数组的C#等价物是什么?

[英]What is the C# equivalent of memcpy array to int?

EDIT: I'm considering this question different from the potential duplicate because none of the answers on that one contain the approach I went with which is the BitConverter class. 编辑:我正在考虑这个问题不同于潜在的重复,因为那个问题的答案都不包含我使用的方法,即BitConverter类。 In case me marking this as not a duplicate gets rid of the potential duplicate question link here it is . 如果我纪念这一不重复摆脱了可能重复的问题链接在这里它是

I'm wondering what the c# equivalent of this code is considering each element in the array is a byte and it's getting copied to an int. 我想知道这个代码的c#等价物是在考虑数组中的每个元素是一个字节并且它被复制到一个int。

byte offsetdata[sizeof(int)] = { 0,0,0,0 };
offsetdata[0] = m_Response[responseIndex++];
offsetdata[1] = m_Response[responseIndex++];
offsetdata[2] = m_Response[responseIndex++];
offsetdata[3] = m_Response[responseIndex++];
int offset = 0;
memcpy(&offset, offsetdata, sizeof offset);
m_ZOffset = offset;

This is possibly a duplicate, but I don't know how to mark it. 这可能是重复的,但我不知道如何标记它。 Here is the original post: memcpy function in c# 这是原帖: c#中的memcpy函数

From the other post there appear to be several options: 在另一篇文章中,似乎有几种选择:

  1. Array.Copy Array.Copy
  2. Object.MemberwiseClone Object.MemberwiseClone
  3. ICloneable Interface ICloneable接口

It really seems you just want to use 它似乎你只是想使用

Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array 返回从字节数组中指定位置的四个字节转换的32位有符号整数

int offset =  BitConverter.ToInt32(somebyteData,0);

Note : Beware of the endianness 注意 :注意字节顺序

Or you could just use the the shift operators 或者您可以使用移位运算符

Though if you want to use memcpy , It depends what overloads you need, you can just P/invoke it if you want 虽然如果你想使用memcpy ,它取决于你需要的重载,你可以根据需要P /调用它

[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);

However, for pointers the last 2 are probably good equivalents, with Marshal.Copy having by far the most versatility 然而,对于指针来说,最后2个可能是很好的等价物, Marshal.Copy具有迄今为止最多功能

Copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset. 将从特定偏移量开始的源数组复制指定数量的字节复制到从特定偏移量开始的目标数组。

Copies a range of elements in one Array to another Array and performs type casting and boxing as required. 将一个Array中的一系列元素复制到另一个Array,并根据需要执行类型转换和装箱。

Copies a number of bytes specified as a long integer value from one address in memory to another. 将指定为长整数值的多个字节从内存中的一个地址复制到另一个地址。

Copies data from a managed array to an unmanaged memory pointer, or from an unmanaged memory pointer to a managed array. 将数据从托管数组复制到非托管内存指针,或从非托管内存指针复制到托管阵列。

In c# you could do something like: 在c#中,您可以执行以下操作:

int offset = m_Response[responseIndex++];
offset <<= 8;
offset |= m_Response[responseIndex++];
offset <<= 8;
...

Finnally in offset you'll have every byte. offset您将获得每个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM