469,304 Members | 2,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

How to do memcpy for Byte[]?

EOS

I guess what I want to do is best explain via the codes here;

Byte[] byteA = new Byte[100000];
Byte[] byteB = new Byte[4];
....
....
....

Now I want to copy 4 bytes from certain portion of the byteA array.

In C/C++, this is what we do;

memcpy(byteB, byteA[X], 4);

Or

memcpy(byteB, byteA+X, 4);

But with C#, I had been stuck for a while with no luck....

Hope you understand what I wanted and could show me the way.

Thanks for your time. Sometime I wish Microsoft could create C# to be nearer
to C/C++. C# is still driving me headache...
Dec 4 '05 #1
6 33353
You can either use one of the overloads of the Array.Copy method, or
Buffer.BlockCopy. MSDN online has all the details and method signatures plus
sample code.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"EOS" wrote:

I guess what I want to do is best explain via the codes here;

Byte[] byteA = new Byte[100000];
Byte[] byteB = new Byte[4];
....
....
....

Now I want to copy 4 bytes from certain portion of the byteA array.

In C/C++, this is what we do;

memcpy(byteB, byteA[X], 4);

Or

memcpy(byteB, byteA+X, 4);

But with C#, I had been stuck for a while with no luck....

Hope you understand what I wanted and could show me the way.

Thanks for your time. Sometime I wish Microsoft could create C# to be nearer
to C/C++. C# is still driving me headache...

Dec 4 '05 #2
"EOS" <no****@nospam.org> wrote in
news:O7**************@TK2MSFTNGP09.phx.gbl:

I guess what I want to do is best explain via the codes here;

Byte[] byteA = new Byte[100000];
Byte[] byteB = new Byte[4];
...
...
...

Now I want to copy 4 bytes from certain portion of the byteA
array.

In C/C++, this is what we do;

memcpy(byteB, byteA[X], 4);

Or

memcpy(byteB, byteA+X, 4);

But with C#, I had been stuck for a while with no luck....

Hope you understand what I wanted and could show me the way.

Thanks for your time. Sometime I wish Microsoft could create C#
to be nearer to C/C++. C# is still driving me headache...

When looking for a method in .Net, first think about what type you
are dealing with. Then look at that type's methods to see if it has
the functionality you desire.

In your case, you are dealing with the System.Array type. It has a
static Copy method that will do what you want:

System.Array.Copy(byteA, 0, byteB, 0, 4);
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Dec 4 '05 #3
EOS <no****@nospam.org> wrote:
I guess what I want to do is best explain via the codes here;


Please see my response in the framework group. Multi-posting questions
is a bad idea in general - it wastes people's time, as people will
respond in one group without seeing that you've already got an anser in
another group.

See http://www.pobox.com/~skeet/csharp/faq/posting.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 4 '05 #4
EOS
Ok... Understood and I will avoid that in the future.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
EOS <no****@nospam.org> wrote:
I guess what I want to do is best explain via the codes here;


Please see my response in the framework group. Multi-posting questions
is a bad idea in general - it wastes people's time, as people will
respond in one group without seeing that you've already got an anser in
another group.

See http://www.pobox.com/~skeet/csharp/faq/posting.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 4 '05 #5
In .NET 2.0 Buffer.BlockCopy is implemented in with the help of unsafe
functions the source looks like classical memcpy :8-)

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:02**********************************@microsof t.com...
You can either use one of the overloads of the Array.Copy method, or
Buffer.BlockCopy. MSDN online has all the details and method signatures
plus
sample code.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"EOS" wrote:

I guess what I want to do is best explain via the codes here;

Byte[] byteA = new Byte[100000];
Byte[] byteB = new Byte[4];
....
....
....

Now I want to copy 4 bytes from certain portion of the byteA array.

In C/C++, this is what we do;

memcpy(byteB, byteA[X], 4);

Or

memcpy(byteB, byteA+X, 4);

But with C#, I had been stuck for a while with no luck....

Hope you understand what I wanted and could show me the way.

Thanks for your time. Sometime I wish Microsoft could create C# to be
nearer
to C/C++. C# is still driving me headache...

Dec 4 '05 #6
EOS
Excuss me, what's wrong with clasical memcpy? I had been using it for my
whole life for C/C++ development and I love it and miss it now C# : (

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:en***************@TK2MSFTNGP11.phx.gbl...
In .NET 2.0 Buffer.BlockCopy is implemented in with the help of unsafe
functions the source looks like classical memcpy :8-)

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:02**********************************@microsof t.com...
You can either use one of the overloads of the Array.Copy method, or
Buffer.BlockCopy. MSDN online has all the details and method signatures
plus
sample code.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"EOS" wrote:

I guess what I want to do is best explain via the codes here;

Byte[] byteA = new Byte[100000];
Byte[] byteB = new Byte[4];
....
....
....

Now I want to copy 4 bytes from certain portion of the byteA array.

In C/C++, this is what we do;

memcpy(byteB, byteA[X], 4);

Or

memcpy(byteB, byteA+X, 4);

But with C#, I had been stuck for a while with no luck....

Hope you understand what I wanted and could show me the way.

Thanks for your time. Sometime I wish Microsoft could create C# to be
nearer
to C/C++. C# is still driving me headache...


Dec 5 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Vince | last post: by
16 posts views Thread by Khuong Dinh Pham | last post: by
26 posts views Thread by Case | last post: by
1 post views Thread by babylon | last post: by
6 posts views Thread by Eric | last post: by
18 posts views Thread by Mark | last post: by
3 posts views Thread by Ashit Vora | last post: by
3 posts views Thread by Andrew Poelstra | last post: by
6 posts views Thread by Ashit Vora | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.