[Mono-list] Marshaling a struct with network byte order?

Robert Jordan robertj at gmx.net
Tue Dec 2 17:01:02 UTC 2014


On 02.12.2014 17:18, Michael McGlothlin wrote:
> Is there any common way to correctly marshal a struct such that the
> resulting bytes are in network byte order? I have a file format that
> requires this order for compatibility. Currently I'm storing each
> chunk as a struct and am using Marshal to convert it to/from bytes.
> Seems to work fine other than being little endian. Is there something
> like a MarshalAs attribute for controlling byte order?

Being part of platform/invoke, Marshal* & friends are not suitable
for byte order conversions because they follow (by design) the
conventions of platform they are working on.

>
> Couldn't find a general .NET/C# way other than doing it with
> Array.Reverse() each time. I thought Mono might have dealt with such
> a cross platform issue itself..

See http://tirania.org/blog/archive/2007/May-15-2.html

Robert




More information about the Mono-list mailing list