4

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. 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.

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;
m4gik
  • 430
  • 5
  • 27
  • 1
    Maybe what you're looking for are the `shift`s operators (`>>`, `<<`) – dcg Mar 28 '19 at 00:35
  • 3
    Possible duplicate of [memcpy function in c#](https://stackoverflow.com/questions/2996487/memcpy-function-in-c-sharp) – Mansoor Mar 28 '19 at 00:37

3 Answers3

2

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

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

[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

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.

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.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

This is possibly a duplicate, but I don't know how to mark it. Here is the original post: memcpy function in c#

From the other post there appear to be several options:

  1. Array.Copy
  2. Object.MemberwiseClone
  3. ICloneable Interface
Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • 2
    There is a __flag__ option at the bottom of the post (in grey, next to the __share__ and __edit__ options). – Mansoor Mar 28 '19 at 00:47
  • 1
    @Mansoor unfortunately, flagging (in this case _duplicates_) requires [3000 rep or more](https://meta.stackexchange.com/questions/118124/where-on-earth-is-the-mark-duplicate-ui) –  Mar 28 '19 at 00:49
  • 1
    @MickyD That explains why I couldn't find it – Salvatore Mar 28 '19 at 00:50
  • 3
    Welcome Matthew. Nice try with your post and saying that you did try to mark as duplicate. Once you get to 3000 rep you can mark as duplicate. I'm sure you will get there in no time. :) Wishing you well. _[Tell me more about privileges](https://stackoverflow.com/help/privileges)_ –  Mar 28 '19 at 00:51
0

In c# you could do something like:

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

Finnally in offset you'll have every byte.

dcg
  • 4,187
  • 1
  • 18
  • 32