Saturday, November 25, 2006

Convert array types with Buffer.BlockCopy - Permalink

If you are searching a high performance way to convert a primitive type array into another, the BlockCopy method of the Buffer class can save the day:




public static void BlockCopy (
Array src,
int srcOffset,
Array dst,
int dstOffset,
int count
)



As an example we will convert an int array into a byte array:

int[] ai = new int[] { 0x11223344, 0x55667788 };
byte[] ab = new byte[ Buffer.ByteLength(ai) ];
Buffer.BlockCopy( ai, 0, ab, 0, ab.Length );


Code by NETMaster (Thomas Scheidegger)

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?