As you already may know,I’m migrating into C# and some things in C++ look different.
C++ code
BYTE packetBuffer[32] = {0}; *(LPWORD)(packetBuffer + 0) = 0xC; *(LPWORD)(packetBuffer + 2) = 0x5000; *(LPDWORD)(packetBuffer + 6) = dwArgs[13]; *(LPDWORD)(packetBuffer + 10) = *(keyArray2 + 0); *(LPDWORD)(packetBuffer + 14) = *(keyArray2 + 1);
Note dwArgs and keyArray2 are ‘array of DWORD’
This is how it’s placed
- packetbuffer[0] will be 0xC
- packetbuffer[1] will be 0x00
- packetbuffer[2] will be 0x50
- packetbuffer[3] will be 0x00
and so on
How to do that in C#?
I tried this,but it doesn’t work
packetBuffer[0] = 0xC; packetBuffer[2] = (byte)0x5000; //error packetBuffer[6] = (byte)dwArgs[13];
You can use
BitConverterto convert data to and from byte arrays. Unfortunately there’s no facility to copy into an existing array. My ownEndianBitConverterin my MiscUtil library allows this if you need it, as well as allowing you to specify the endianness to use of course. (BitConverteris usually little endian in .NET – you can check it with theIsLittleEndianfield.)For example:
etc. The cast to
intin the second call toCopyBytesis redundant, but included for clarity (bearing in mind the previous line!).EDIT: Another alternative if you’d rather stick with the .NET standard libraries, you might want to use
BinaryWriterwith aMemoryStream.