C++ -> C#; MemCpy -> BlockCopy; Arrgghhh!

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:

memcpy(buffer2, buffer1, count) (count = buffer1.length * sizeof(unsigned char) ; 1 byte)

Output:

arrayElement[0] = 1414743380
arrayElement[1] = 0
arrayElement[2] = 0
the rest are zero...


Copying {'T','E','S','T'} (char Array -> uint Array) using Buffer.BlockCopy:

Buffer.BlockCopy(buffer1, 0, buffer2, 0, count); (count = buffer1.length * sizeof(char) ; 2 bytes)

Output:

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

Signed, Weary... :-)
Last edited on
Note that the count should be in number of bytes.
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...
Last edited on
count = length * sizeof(unsigned long int);
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.

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

The original post, above, has been corrected.
Last edited on
closed account (z05DSL3A)
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);
NB: untested code
Last edited on
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...?
Last edited on
closed account (z05DSL3A)
C++: a char is 8bits and a uint is 32bits so four chars can be packed into one uint.

C#: a char is 16bits and a uint is 32bits so only two chars can be packed into one uint.

The reason why the numbers look different in your output is that the chars are encoded differently in C++ and C#.
Thank you for the response. I just saw your code snippet above... I will give it a try. Thank you!

'Thanks' to you and Peter87 for taking the time to respond. :-)
The snippet you provided works.

The arrays now 'match':

memcpy(buffer2, buffer1, count)

Output:

arrayElement[0] = 1414743380
the rest are zero...

Buffer.BlockCopy(buffer1, 0, buffer2, 0, count);

Output:

arrayElement[0] = 4522068
the rest are zero...

The encoding will give me the visual aspect that I need for easier comparison.

Thank you very much! :-)
Last edited on
Topic archived. No new replies allowed.