I am baffled as to why using Buffer.BlockCopy in C# is yielding different result than C++ MemCpy, using the 'same' arguments?
The C++ array SOURCE elements are of a type unsigned char, and the C++ array DESTINATION elements are of type unsigned long int.
The C# array SOURCE elements are of a type char, and the C# array DESTINATION elements are of type uint.
The C# app using Buffer.BlockCopy is running on Win7/x64 and the platform target (Visual Studio Build Option) is set to x86.
The C++ app using MemCpy is running on XP/x86.
The C++ is MemCpy logic is echoing the desired output. I am wanting the Buffer.BlockCopy behavior/output, to match the MemCpy behaviour/output.
Copying {'T','E','S','T'} (char Array -> uint Array) using MemCpy:
arrayElement[0] = 4522068
arrayElement[1] = 5505107
arrayElement[2] = 0
the rest are zero...
I know that there are a ton of variables, but I am thinking that somebody is going to glance at the output from both operations and come to an obvious conclusion that has not occured to me?
Thank you for the response. Please see above, as I have added commentary for that which you have mentioned. Unless, there is another more diplomatic meaning to your response - as I suspect that there is a fundamental gap in my knowledge that is preventing me from seeing the obvious... and it does not help that I am needing to get past this, and move-on to another task...
I changed the original post to reflect that the MemCpy count argument is an unsigned char. While appreciated, the statement you supplied would not work, and the MemCpy is part of the original C++ working logic, wherein count equalled count * sizeof(buffer1).
I understand that I have only provided a small part of the puzzle - and do, appreciate your input.
The last argument to memcpy is the number of bytes you want to copy. If the number of elements is length and each element is sizeof(unsigned long int) bytes the total number of bytes is length * sizeof(unsigned long int).
I don't know much about C# but I think you have the same problem with BlockCopy.
I am sorry - I was not completely clear - I changed the original post to rectify the shortcoming. Thank you Peter87 - I think I am posting this after I have already received another response, so hopefully 'things' won't get too confusing...
I now understand what had led you to your conclusion - In my example, I had the MemCpy arguments reversed. This oversite only applies to the example above - they are correct in the code that generated the sample output.
char in C# is 16bits and uint is 32bits, so it takes two uints to sore your four chars.
You could try something like:
1 2 3 4 5 6
string value = "TEST";
// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
Buffer.BlockCopy(asciiBytes, 0, buffer2, 0, 4);
Your patience is appreciated... I understand what you are saying, but...
Why can I not discern a mathematical, 1:2 relationship between the two outputs?
More important to me: Why does one array 'wrap' differently than the other? Both destination arrays hold 4294967290 bytes? I would expect a binary copy to fill an array element, and move to the next, yet the second array 'wraps' earlier...?