Click to See Complete Forum and Search --> : memcpy


yarivabr
May 4th, 2009, 06:05 AM
Hello,

I have a problem in porting a few code lines from C++ to C#.

in C++:
std::vector<char> vctNtwrkS;
vctNtwrkS.resize(4);
int nCounterS = 224;
nCounterS = htonl(nCounterS);
memcpy (&vctNtwrkS[0], &nCounterS, 4);

In C#:
int nSequence = 224;
nSequence = System.Net.IPAddress.HostToNetworkOrder(nSequence);
byte[] mb = new byte[4];
THIS IS THE MISSING PIECE: I need to do the memcpy. I know there isn't memcpy in C# and I looked for example on web and couldn't find this.
In addition: in C# char type is a unicode so inorder to do the same I need to transfer char to byte, right ?

Thanks, Yariv

cilu
May 4th, 2009, 06:58 AM
See the BitConverter class.

byte [] mb = BitConverter.GetBytes(nSequence);

yarivabr
May 11th, 2009, 05:51 AM
Thanks