[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Converting big-endian to litle-endian

Andreas

2/2/2007 12:45:00 PM

Hi, Im filling a known structure with data from a byte-array and I know that
the values are represented in big-endian. Would it be possible to make this
conversion "on the fly" ? Example structure

[StructLayout(LayoutKind.Sequential)]
public struct MyStructure
{
public ushort Width;
public ushort Height;
}

Im using this code to populate the struct with data from a byte-array thats
retrieved from a filestream

public static T ReadStruct<T>(FileStream fs, int offset)
{
byte[] buffer =
new byte[Marshal.SizeOf(typeof(T))];
fs.Position = offset;

fs.Read(buffer, 0, Marshal.SizeOf(typeof(T)));
GCHandle handle =
GCHandle.Alloc(buffer, GCHandleType.Pinned);

T populatedItem =
(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
handle.Free();

return populatedItem;
}

It used like this

MyStruct s = ReadStruct<MyStruct>(fs, 0);

Thanks!

//Andreas


1 Answer

(Mattias Sjögren)

2/2/2007 7:36:00 PM

0


>Hi, Im filling a known structure with data from a byte-array and I know that
>the values are represented in big-endian. Would it be possible to make this
>conversion "on the fly" ? Example structure

Wouldn't it be easier to use the BitConverter class for this? You can
find one with big endian support here

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


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.