Tag: 字节序

使用C#在Little或Big Endian中写入/读取二进制数据的最佳方法?

好的,如果我在.NET下有一个用little endian或big endian编码的二进制文件,那么读/写它的最佳方法是什么? 在.NET框架中,我只设法找到使用little endian作为默认值的BinaryWritters / BinaryReaders,所以我的方法是实现我自己的BinaryReader / BinaryWritter来读取/写入big endian中的数据,但是我想知道是否有更好的方法。

ASM中快速的little-endian到big-endian转换

我在C#中有一个uint类型数组,在检查程序是否在小端机器上运行之后,我想将数据转换为big-endian类型。 因为数据量可以变得非常大但总是均匀,我想考虑将两种uint类型作为ulong类型,以获得更好的性能并在ASM中对其进行编程,所以我搜索的速度非常快(如果可能的话,速度最快) )汇编程序算法转换big-endian的big-endian。

C#Big-endian ulong来自4个字节

我试图在C#中将4字节数组转换为ulong。 我目前正在使用此代码: atomSize = BitConverter.ToUInt32(buffer, 0); 字节[4]包含: 0 0 0 32 但是,字节是Big-Endian。 有没有一种简单的方法可以将这个Big-Endian ulong转换为Little-Endian ulong?

Marshal.PtrToStructure(以及返回)和字节顺序交换的通用解决方案

我有一个系统,远程代理发送序列化结构(来自嵌入式C系统)供我通过IP / UDP读取和存储。 在某些情况下,我需要发回相同的结构类型。 我以为我使用Marshal.PtrToStructure(接收)和Marshal.StructureToPtr(发送)进行了很好的设置。 但是,一个小问题是网络大端整数需要转换为我的x86小端格式才能在本地使用。 当我再次发送它们时,大端是可行的方式。 以下是有问题的function: private static T BytesToStruct(ref byte[] rawData) where T: struct { T result = default(T); GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned); try { IntPtr rawDataPtr = handle.AddrOfPinnedObject(); result = (T)Marshal.PtrToStructure(rawDataPtr, typeof(T)); } finally { handle.Free(); } return result; } private static byte[] StructToBytes(T data) where T: struct { byte[] […]