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.

Re: How to do simple memcpy in C#? by Peter

Peter
Sun Jun 05 05:01:11 CDT 2005

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
Windows Embedded MVP
http://www.inthehand.com | http://www.peterfoot.net |
http://www.opennetcf.org

"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.
>
>
>



Re: How to do simple memcpy in C#? by Jon

Jon
Sun Jun 05 07:22:42 CDT 2005

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: How to do simple memcpy in C#? by Gravity

Gravity
Mon Jun 06 09:20:12 CDT 2005

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.

"Jon Skeet [C# MVP]" <skeet@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 - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Re: How to do simple memcpy in C#? by Willy

Willy
Mon Jun 06 11:02:55 CDT 2005


"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.




Re: How to do simple memcpy in C#? by Gravity

Gravity
Sat Jun 11 06:38:27 CDT 2005

Thanks for the explanation.

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

"Willy Denoyette [MVP]" <willy.denoyette@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.
>
>
>



Re: How to do simple memcpy in C#? by Frank

Frank
Sat Jun 11 07:19:39 CDT 2005

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


"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.
>
>
>