469,317 Members | 1,905 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Howto convert a byte array to double array.

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
Nov 16 '05 #1
5 18110
kevinniu <ke******@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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[i] <<= 1;
return 0;
}

--
Nadav
http://www.ddevel.com
"kevinniu" wrote:
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

Nov 16 '05 #3
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" <Na***@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
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[i] <<= 1;
return 0;
}

--
Nadav
http://www.ddevel.com
"kevinniu" wrote:
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

Nov 16 '05 #4
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
kevinniu <ke******@hotmail.com> wrote:
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.

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

Nov 16 '05 #5
Lin Liang <l.******@pgrad.unimelb.edu.au> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by matt melton | last post: by
15 posts views Thread by jeko | last post: by
3 posts views Thread by Michael | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
6 posts views Thread by Dennis | last post: by
1 post views Thread by Paul Jarvis | last post: by
1 post views Thread by CARIGAR | 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
By using this site, you agree to our Privacy Policy and Terms of Use.