Groups | Blog | Home
all groups > dotnet general > november 2003 >

dotnet general : Raw copy from array of byte to array of Int16


TDH
11/22/2003 10:21:04 PM
Hi,

I need to do a raw copy of a byte array to an Int16 array.
For e.g., I have an byte array which has length 100, and I
need to copy this to an Int16 array that has length 50. So
2 elements in the byte array gets copied to 1 element in
the Int16 array.

In VB6, this was easily done using CopyMemory. I need
something in .NET that works as fast as CopyMemory. I
can't loop thru each element in the array because the
array could have thousands of elements and looping would
be too slow.

In VB.Net, the CopyMemory function no longer works
correctly. I can call the function, but it does not
actually perform the copy. I think there are some
marshaling calls that would eventually make it work, but
I'd prefer to find a native .NET solution.

I have looked at the members of the Array class, but they
only allow copying item-to-item. They don't allow raw copy
so that 2 elements of the byte array would go into 1
element of the Int16 array.

Does anybody know how to do this kind of raw copying from
one array to another?

Thanks.

Jon Skeet [C# MVP]
11/23/2003 5:13:16 PM
[quoted text, click to view]

Have a look at Buffer.BlockCopy.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
TrongAn Hoang
11/23/2003 10:43:17 PM
Thanks. That did the trick.

[quoted text, click to view]

AddThis Social Bookmark Button