Converting byte[] to structure without Marshal?
Author Message
Converting byte[] to structure without Marshal?

If I have a structure, say

struct foo { int a; int b; int c; }

and an array of 12 bytes which I know contains three integers, say

byte[] pb;

then is there a _tidy_ way to convert from one to the other?

Currently I can see two solutions:

1. Use BitConverter;

myFoo.a = BitConverter.ToInt32(pb, 0);
myFoo.b = BitConverter.ToInt32(pb, 4);
myFoo.c = BitConverter.ToInt32(pb, 8);

This works okay here, but gets pretty ugly with larger/more complex
structures.

2. Use Marshal.PtrToStructure; something like:

IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
Marshal.Copy(..from pb to pb1..);
Marshal.PtrToStructure(..from pb1 to myFoo..);
Marshal.FreeHGlobal(pb1);

This works, but it seems pretty messy that I need to use Marshal to do
this sort of thing.

Is there a C#-native way of doing this?

thanks,

-- dan

Sat, 19 Jun 2004 02:45:42 GMT
Converting byte[] to structure without Marshal?
Use unsafe code.

Foo myFoo;
unsafe (byte* pBuffer = buffer)
{
myFoo = *(myFoo*) pBuffer;

Quote:
}

That's assuming you can use unsafe code in your scenario.

--
This posting is provided "AS IS" with no warranties, and confers no rights.

Quote:
> If I have a structure, say

> struct foo { int a; int b; int c; }

> and an array of 12 bytes which I know contains three integers, say

> byte[] pb;

>  then is there a _tidy_ way to convert from one to the other?

>  Currently I can see two solutions:

>  1. Use BitConverter;

>  myFoo.a = BitConverter.ToInt32(pb, 0);
>  myFoo.b = BitConverter.ToInt32(pb, 4);
>  myFoo.c = BitConverter.ToInt32(pb, 8);

>  This works okay here, but gets pretty ugly with larger/more complex
> structures.

>  2. Use Marshal.PtrToStructure; something like:

>  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
>  Marshal.Copy(..from pb to pb1..);
>  Marshal.PtrToStructure(..from pb1 to myFoo..);
>  Marshal.FreeHGlobal(pb1);

>  This works, but it seems pretty messy that I need to use Marshal to do
> this sort of thing.

>  Is there a C#-native way of doing this?

>  thanks,

>  -- dan

Sat, 19 Jun 2004 03:46:46 GMT
Converting byte[] to structure without Marshal?
Hey Eric,

pBuffer is a byte*? How do you set the byte* pointer to a byte[]?

Josh

Quote:
> Use unsafe code.

> Foo myFoo;
> unsafe (byte* pBuffer = buffer)
> {
>     myFoo = *(myFoo*) pBuffer;
> }

> That's assuming you can use unsafe code in your scenario.

> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.

> > If I have a structure, say

> > struct foo { int a; int b; int c; }

> > and an array of 12 bytes which I know contains three integers, say

> > byte[] pb;

> >  then is there a _tidy_ way to convert from one to the other?

> >  Currently I can see two solutions:

> >  1. Use BitConverter;

> >  myFoo.a = BitConverter.ToInt32(pb, 0);
> >  myFoo.b = BitConverter.ToInt32(pb, 4);
> >  myFoo.c = BitConverter.ToInt32(pb, 8);

> >  This works okay here, but gets pretty ugly with larger/more complex
> > structures.

> >  2. Use Marshal.PtrToStructure; something like:

> >  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
> >  Marshal.Copy(..from pb to pb1..);
> >  Marshal.PtrToStructure(..from pb1 to myFoo..);
> >  Marshal.FreeHGlobal(pb1);

> >  This works, but it seems pretty messy that I need to use Marshal to do
> > this sort of thing.

> >  Is there a C#-native way of doing this?

> >  thanks,

> >  -- dan

Sat, 19 Jun 2004 04:03:59 GMT
Converting byte[] to structure without Marshal?

Quote:
> Use unsafe code.

> Foo myFoo;
> unsafe (byte* pBuffer = buffer)
> {
>     myFoo = *(myFoo*) pBuffer;
> }

> That's assuming you can use unsafe code in your scenario.

Ah, that makes sense -- thanks!

-- dan

Sat, 19 Jun 2004 04:17:26 GMT
Converting byte[] to structure without Marshal?
That's what happens as part of the unsafe.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncs...
l/csharp11152001.asp

--
This posting is provided "AS IS" with no warranties, and confers no rights.

Quote:
> Hey Eric,

> pBuffer is a byte*? How do you set the byte* pointer to a byte[]?

> Josh

> > Use unsafe code.

> > Foo myFoo;
> > unsafe (byte* pBuffer = buffer)
> > {
> >     myFoo = *(myFoo*) pBuffer;
> > }

> > That's assuming you can use unsafe code in your scenario.

> > --
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.

> > > If I have a structure, say

> > > struct foo { int a; int b; int c; }

> > > and an array of 12 bytes which I know contains three integers, say

> > > byte[] pb;

> > >  then is there a _tidy_ way to convert from one to the other?

> > >  Currently I can see two solutions:

> > >  1. Use BitConverter;

> > >  myFoo.a = BitConverter.ToInt32(pb, 0);
> > >  myFoo.b = BitConverter.ToInt32(pb, 4);
> > >  myFoo.c = BitConverter.ToInt32(pb, 8);

> > >  This works okay here, but gets pretty ugly with larger/more complex
> > > structures.

> > >  2. Use Marshal.PtrToStructure; something like:

> > >  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
> > >  Marshal.Copy(..from pb to pb1..);
> > >  Marshal.PtrToStructure(..from pb1 to myFoo..);
> > >  Marshal.FreeHGlobal(pb1);

> > >  This works, but it seems pretty messy that I need to use Marshal to
do
> > > this sort of thing.

> > >  Is there a C#-native way of doing this?

> > >  thanks,

> > >  -- dan

Sat, 19 Jun 2004 04:27:08 GMT
Converting byte[] to structure without Marshal?
No I mean I put your code in VS and it complained about not being able to
convert it, but then I realized the right (I think) syntax is:

unsafe
{
fixed (...)
{
...

Quote:
}
}

rather than unsafe (...) { }

Josh

Quote:
> That's what happens as part of the unsafe.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncs...
Quote:
> l/csharp11152001.asp

> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.

> > Hey Eric,

> > pBuffer is a byte*? How do you set the byte* pointer to a byte[]?

> > Josh

> > > Use unsafe code.

> > > Foo myFoo;
> > > unsafe (byte* pBuffer = buffer)
> > > {
> > >     myFoo = *(myFoo*) pBuffer;
> > > }

> > > That's assuming you can use unsafe code in your scenario.

> > > --
> > > This posting is provided "AS IS" with no warranties, and confers no
> > rights.

> > > > If I have a structure, say

> > > > struct foo { int a; int b; int c; }

> > > > and an array of 12 bytes which I know contains three integers, say

> > > > byte[] pb;

> > > >  then is there a _tidy_ way to convert from one to the other?

> > > >  Currently I can see two solutions:

> > > >  1. Use BitConverter;

> > > >  myFoo.a = BitConverter.ToInt32(pb, 0);
> > > >  myFoo.b = BitConverter.ToInt32(pb, 4);
> > > >  myFoo.c = BitConverter.ToInt32(pb, 8);

> > > >  This works okay here, but gets pretty ugly with larger/more complex
> > > > structures.

> > > >  2. Use Marshal.PtrToStructure; something like:

> > > >  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
> > > >  Marshal.Copy(..from pb to pb1..);
> > > >  Marshal.PtrToStructure(..from pb1 to myFoo..);
> > > >  Marshal.FreeHGlobal(pb1);

> > > >  This works, but it seems pretty messy that I need to use Marshal to
> do
> > > > this sort of thing.

> > > >  Is there a C#-native way of doing this?

> > > >  thanks,

> > > >  -- dan

Sat, 19 Jun 2004 05:31:34 GMT
Converting byte[] to structure without Marshal?
Correct.

I've been ill recently...

--
This posting is provided "AS IS" with no warranties, and confers no rights.

Quote:
> No I mean I put your code in VS and it complained about not being able to
> convert it, but then I realized the right (I think) syntax is:

> unsafe
> {
> fixed (...)
> {
> ...
> }
> }

> rather than unsafe (...) { }

> Josh

> > That's what happens as part of the unsafe.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncs...

- Show quoted text -

Quote:
> > l/csharp11152001.asp

> > --
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.

> > > Hey Eric,

> > > pBuffer is a byte*? How do you set the byte* pointer to a byte[]?

> > > Josh

message

> > > > Use unsafe code.

> > > > Foo myFoo;
> > > > unsafe (byte* pBuffer = buffer)
> > > > {
> > > >     myFoo = *(myFoo*) pBuffer;
> > > > }

> > > > That's assuming you can use unsafe code in your scenario.

> > > > --
> > > > This posting is provided "AS IS" with no warranties, and confers no
> > > rights.

> > > > > If I have a structure, say

> > > > > struct foo { int a; int b; int c; }

> > > > > and an array of 12 bytes which I know contains three integers, say

> > > > > byte[] pb;

> > > > >  then is there a _tidy_ way to convert from one to the other?

> > > > >  Currently I can see two solutions:

> > > > >  1. Use BitConverter;

> > > > >  myFoo.a = BitConverter.ToInt32(pb, 0);
> > > > >  myFoo.b = BitConverter.ToInt32(pb, 4);
> > > > >  myFoo.c = BitConverter.ToInt32(pb, 8);

> > > > >  This works okay here, but gets pretty ugly with larger/more
complex
> > > > > structures.

> > > > >  2. Use Marshal.PtrToStructure; something like:

> > > > >  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
> > > > >  Marshal.Copy(..from pb to pb1..);
> > > > >  Marshal.PtrToStructure(..from pb1 to myFoo..);
> > > > >  Marshal.FreeHGlobal(pb1);

> > > > >  This works, but it seems pretty messy that I need to use Marshal
to
> > do
> > > > > this sort of thing.

> > > > >  Is there a C#-native way of doing this?

> > > > >  thanks,

> > > > >  -- dan

Mon, 21 Jun 2004 01:26:29 GMT
Converting byte[] to structure without Marshal?
No problem! Thanks for all of your help :)

- Josh

Quote:
> Correct.

> I've been ill recently...

> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.

> > No I mean I put your code in VS and it complained about not being able
to
> > convert it, but then I realized the right (I think) syntax is:

> > unsafe
> > {
> > fixed (...)
> > {
> > ...
> > }
> > }

> > rather than unsafe (...) { }

> > Josh

> > > That's what happens as part of the unsafe.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncs...

- Show quoted text -

Quote:
> > > l/csharp11152001.asp

> > > --
> > > This posting is provided "AS IS" with no warranties, and confers no
> > rights.

> > > > Hey Eric,

> > > > pBuffer is a byte*? How do you set the byte* pointer to a byte[]?

> > > > Josh

> message

> > > > > Use unsafe code.

> > > > > Foo myFoo;
> > > > > unsafe (byte* pBuffer = buffer)
> > > > > {
> > > > >     myFoo = *(myFoo*) pBuffer;
> > > > > }

> > > > > That's assuming you can use unsafe code in your scenario.

> > > > > --
> > > > > This posting is provided "AS IS" with no warranties, and confers
no
> > > > rights.

> > > > > > If I have a structure, say

> > > > > > struct foo { int a; int b; int c; }

> > > > > > and an array of 12 bytes which I know contains three integers,
say

> > > > > > byte[] pb;

> > > > > >  then is there a _tidy_ way to convert from one to the other?

> > > > > >  Currently I can see two solutions:

> > > > > >  1. Use BitConverter;

> > > > > >  myFoo.a = BitConverter.ToInt32(pb, 0);
> > > > > >  myFoo.b = BitConverter.ToInt32(pb, 4);
> > > > > >  myFoo.c = BitConverter.ToInt32(pb, 8);

> > > > > >  This works okay here, but gets pretty ugly with larger/more
> complex
> > > > > > structures.

> > > > > >  2. Use Marshal.PtrToStructure; something like:

> > > > > >  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
> > > > > >  Marshal.Copy(..from pb to pb1..);
> > > > > >  Marshal.PtrToStructure(..from pb1 to myFoo..);
> > > > > >  Marshal.FreeHGlobal(pb1);

> > > > > >  This works, but it seems pretty messy that I need to use
Marshal
> to
> > > do
> > > > > > this sort of thing.

> > > > > >  Is there a C#-native way of doing this?

> > > > > >  thanks,

> > > > > >  -- dan

Mon, 21 Jun 2004 02:30:42 GMT
Converting byte[] to structure without Marshal?
Is recommending developer write C# code with unsafe methods a good thing?
Might as well just go back to C/C++ and memory overwrite hell.    I
understand that using unsafe method is important is some rare scenarios, but
seems to be an overkill just to convert a structure.

What is the best method to solve the original problem with only "safe" C#
code?

Quote:
> Use unsafe code.

> Foo myFoo;
> unsafe (byte* pBuffer = buffer)
> {
>     myFoo = *(myFoo*) pBuffer;
> }

> That's assuming you can use unsafe code in your scenario.

> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.

> > If I have a structure, say

> > struct foo { int a; int b; int c; }

> > and an array of 12 bytes which I know contains three integers, say

> > byte[] pb;

> >  then is there a _tidy_ way to convert from one to the other?

> >  Currently I can see two solutions:

> >  1. Use BitConverter;

> >  myFoo.a = BitConverter.ToInt32(pb, 0);
> >  myFoo.b = BitConverter.ToInt32(pb, 4);
> >  myFoo.c = BitConverter.ToInt32(pb, 8);

> >  This works okay here, but gets pretty ugly with larger/more complex
> > structures.

> >  2. Use Marshal.PtrToStructure; something like:

> >  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
> >  Marshal.Copy(..from pb to pb1..);
> >  Marshal.PtrToStructure(..from pb1 to myFoo..);
> >  Marshal.FreeHGlobal(pb1);

> >  This works, but it seems pretty messy that I need to use Marshal to do
> > this sort of thing.

> >  Is there a C#-native way of doing this?

> >  thanks,

> >  -- dan

Mon, 28 Jun 2004 00:38:47 GMT
Converting byte[] to structure without Marshal?

Quote:

>[...]
> What is the best method to solve the original problem with only "safe" C#
> code?

The original poster's solutions are safe.  In converting a byte[] to a managed
structure containing arrays of non-built-in types, I've found no better way
than to write field-by-field conversion code which uses BitConverter.  (I got
no responses to my "Marshal a complex struct?" post

Quote:

> > > If I have a structure, say

> > > struct foo { int a; int b; int c; }

> > > and an array of 12 bytes which I know contains three integers, say

> > > byte[] pb;

> > >  then is there a _tidy_ way to convert from one to the other?

> > >  Currently I can see two solutions:

> > >  1. Use BitConverter;

> > >  myFoo.a = BitConverter.ToInt32(pb, 0);
> > >  myFoo.b = BitConverter.ToInt32(pb, 4);
> > >  myFoo.c = BitConverter.ToInt32(pb, 8);

> > >  This works okay here, but gets pretty ugly with larger/more complex
> > > structures.

> > >  2. Use Marshal.PtrToStructure; something like:

> > >  IntPtr pb1 = Marshal.AllocHGlobal(sizeof(myFoo));
> > >  Marshal.Copy(..from pb to pb1..);
> > >  Marshal.PtrToStructure(..from pb1 to myFoo..);
> > >  Marshal.FreeHGlobal(pb1);

> > >  This works, but it seems pretty messy that I need to use Marshal to do
> > > this sort of thing.

> > >  Is there a C#-native way of doing this?

--
To send mail, please use my surname where indicated:

Mon, 28 Jun 2004 02:17:42 GMT

 Page 1 of 1 [ 10 post ]

Relevant Pages