How to do simple memcpy in C#?

  • Thread starter Thread starter Gravity
  • Start date Start date
G

Gravity

Hi,

I am very new to C#? How could I do a simple memcpy (in C/C++) in C#?

Example; I have a Byte[] Data = new Byte[500];

And I want to copy various things like int32, other bytes etc into various
section of the Data[]. How do I do that?

With C/C++ is rather simple, which is memcpy( ).

Thanks for readings and hope for some advices.
 
Take a look at the System.BitConverter which contains a number of methods
for extracting numerical types out of byte arrays, and getting the byte
array representation of numbers that can be copied in.

Peter
 
Peter Foot said:
Take a look at the System.BitConverter which contains a number of methods
for extracting numerical types out of byte arrays, and getting the byte
array representation of numbers that can be copied in.

Note that if you want to copy lots of things into the same array, it
may be more efficient to use a MemoryStream and BinaryWriter, as
BitConverter will always create a new byte array for each thing it is
asked to convert to bytes.

I have my own version of BitConverter (which allows either endianness,
although it's irrelevant here) which allows you to copy the bytes into
an existing byte array.

See http://www.pobox.com/~skeet/csharp/miscutil
 
Thanks Jon, I think the MemoryStream should be able to do what I want.

I am a bit dissapointed on the C# .net, almost everything I learn in C/C++
cannot be used.

Since the name C#, sometime I hope they are more C/C++ friendly. But I do
understand it is due to the different architecture.

Thanks again.
 
Gravity said:
Thanks Jon, I think the MemoryStream should be able to do what I want.

I am a bit dissapointed on the C# .net, almost everything I learn in C/C++
cannot be used.

Since the name C#, sometime I hope they are more C/C++ friendly. But I do
understand it is due to the different architecture.

Thanks again.

What do you mean with this?
memcpy is a C library function and has strictly nothing to do with the C
language per se.
C# (the language) is not meant to be C (the language) compatible. The C#
language is meant to offer an alternative for C++ (the languages) and
carries a lot of the syntax and semantics of C++ (just like Java). C# is an
OO language where people are forced to apply OO design patterns, there is no
alternative (unlike C++, where most developers use it simply like a better
C) everything in C# is an object and low level functions like those offered
by the C library (memcpy etc..) don't apply and aren't of any use, the
Framework Class Library offers OO based alternatives.
So I suggest you to change your mindset, C# is not a C clone, if this is
what you expect you will get disappointed, if you look at it as a new
language and paradigm, and pay much attention to the FCL which is key in
this environment and far more important than the languages themselves, you
will get a good feeling and probably forget about C very quickly.

Willy.
 
The Buffer class is nearly identical to memcpy, except it only operates on
primitives. It is ideal if you need to copy large arrays.


Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor
 
Back
Top