Home All Groups Group Topic Archive Search About

How to do simple memcpy in C#?

Author
5 Jun 2005 9:31 AM
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.

Author
5 Jun 2005 10:01 AM
Peter Foot [MVP]
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

Show quoteHide quote
"Gravity" <gravity@nospam.org> wrote in message
news:egt%23OFbaFHA.1040@TK2MSFTNGP10.phx.gbl...
> 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.
>
>
>
Are all your drivers up to date? click for free checkup

Author
5 Jun 2005 12:22 PM
Jon Skeet [C# MVP]
Peter Foot [MVP] <feedback@nospam-inthehand.com> wrote:
> 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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Author
6 Jun 2005 2:20 PM
Gravity
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.

Show quoteHide quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1d0cfeee891bb37b98c26d@msnews.microsoft.com...
> Peter Foot [MVP] <feedback@nospam-inthehand.com> wrote:
>> 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
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
Author
6 Jun 2005 4:02 PM
Willy Denoyette [MVP]
"Gravity" <gravity@nospam.org> wrote in message
news:%23WvpeLqaFHA.2076@TK2MSFTNGP15.phx.gbl...
> 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.
Author
11 Jun 2005 11:38 AM
Gravity
Thanks for the explanation.

Whatever it is.... I just have to live with it.

Show quoteHide quote
"Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote in message
news:ut6Y1EraFHA.3364@TK2MSFTNGP09.phx.gbl...
>
> "Gravity" <gravity@nospam.org> wrote in message
> news:%23WvpeLqaFHA.2076@TK2MSFTNGP15.phx.gbl...
>> 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.
>
>
>
Author
11 Jun 2005 12:19 PM
Frank Hileman
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


Show quoteHide quote
"Gravity" <gravity@nospam.org> wrote in message
news:egt%23OFbaFHA.1040@TK2MSFTNGP10.phx.gbl...
> 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.
>
>
>

Bookmark and Share