cast byte[] to various managed types

S

sklett

I have a byte[] returned from an unmanaged dll. If I was in c/c++ I would
do something like this:

void* pData; // a pointer to a struct with various types

int i;
int* i = &i;
*i = *pData;

or something like that, it's been awhile...

but in c#, I can't find the solution. I have looked at Convert.ToInt32 but
it will only take a single byte type which isn't enough.

I tried using unsafe but didn't find success.

I have google and googled, but nothing is coming up for me solution. I
found one article that sounded promising utilizing BinaryFormatter, but
wasn't able to get it to work.

Anyone have an example how to case an array of bytes to various managed
types?

Thanks,
Steve
 
D

Daniel O'Connell [C# MVP]

sklett said:
I have a byte[] returned from an unmanaged dll. If I was in c/c++ I would
do something like this:

void* pData; // a pointer to a struct with various types

int i;
int* i = &i;
*i = *pData;

or something like that, it's been awhile...

but in c#, I can't find the solution. I have looked at Convert.ToInt32
but it will only take a single byte type which isn't enough.

Look at the BitConverter class, it'll probably let you do most of what you
need to do(You might have some endianess problems, in which case Jon Skeet's
EndianessBitConverter[1] might be of use).

1. http://www.yoda.arachsys.com/csharp/miscutil/
 

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