|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to do simple memcpy in C#?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 -- Show quoteHide quotePeter 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. > > > Peter Foot [MVP] <feedback@nospam-inthehand.com> wrote:
> Take a look at the System.BitConverter which contains a number of methods Note that if you want to copy lots of things into the same array, it > for extracting numerical types out of byte arrays, and getting the byte > array representation of numbers that can be copied in. 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 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 "Gravity" <gravity@nospam.org> wrote in message What do you mean with this?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. 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. 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. > > > 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. > > >
Other interesting topics
A challenge to all MVP's.......GET A J.O.B. for once in your life
Windows service Unable to access mapped drives on XP and 2003 Boxe My application works fine in debug mode but does not work in release mode. Parameter Methods Performance (val,ref,out) Text Files, Encoding and NewLine character Strange Excel importing problem Process.Start("MyComputer"); Checking for implementation of an interface Simple architecture question. Detecting overriding with reflection? |
|||||||||||||||||||||||