Howto convert a byte array to double array.

K

kevinniu

Hi everyone,

In c#, what is the fastest way(include unsafe) to

convert a array of bytes(which really contains the

bytes of a double array) to a arry of double.

thanks

kn
 
J

Jon Skeet [C# MVP]

kevinniu said:
In c#, what is the fastest way(include unsafe) to
convert a array of bytes(which really contains the
bytes of a double array) to a arry of double.

Try Buffer.BlockCopy - that should be pretty fast.
 
G

Guest

if you want to prvent the copying use mixed mode C++, using unmanaged C++ you can get a void* pointer to the managed buffer, then you can do with it whatever you want...

Mixed mode C++ array manipulation ( access a System::Byte array as an 'unsigned short' array):
virtual int ManipArray(System::Byte btArray __gc[])
{
int i = 0;
BYTE __pin *pbtArray = &btArray[0];
unsigned short *uiArray = (unsigned short*)pbtArray;
for(i; i < btArray->Length/2; i++)
uiArray <<= 1;
return 0;
}
 
L

Lin Liang

Nadav,

Thanks for the reply.

Is it possible to use unsafe code in C# to get the same result?

You know I am programming in C# and I am not familiar with mixed C++.

Any idea?

Regards

kevin

Nadav said:
if you want to prvent the copying use mixed mode C++, using unmanaged C++
you can get a void* pointer to the managed buffer, then you can do with it
whatever you want...
Mixed mode C++ array manipulation ( access a System::Byte array as an 'unsigned short' array):
virtual int ManipArray(System::Byte btArray __gc[])
{
int i = 0;
BYTE __pin *pbtArray = &btArray[0];
unsigned short *uiArray = (unsigned short*)pbtArray;
for(i; i < btArray->Length/2; i++)
uiArray <<= 1;
return 0;
}

--
Nadav
http://www.ddevel.com


kevinniu said:
Hi everyone,

In c#, what is the fastest way(include unsafe) to

convert a array of bytes(which really contains the

bytes of a double array) to a arry of double.

thanks

kn
 
L

Lin Liang

Jon,

Thanks for your reply.

I have tried with the BlockCopy, as I have a lot of data,

I really need to speed thing up..

Any other ways to improve the performance?

Best Regards

Kevin
 
J

Jon Skeet [C# MVP]

Lin Liang said:
Is it possible to use unsafe code in C# to get the same result?

You know I am programming in C# and I am not familiar with mixed C++.

Any idea?

I should think you could just use unsafe code and write almost exactly
the same code in C#. See a tutorial on unsafe code for more
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top