Is there a generic way to write a struct to bytes in Big Endian format?

RLH

I've found questions such as this one, which have come close to solving my dilemma. However, I've yet to find a clean approach to solving this issue in a generic manner.

I have a project that has a lot of structs that will be used for binary data transmission. This data needs to be Big Endian and, of course, most .Net architecture is Little Endian. This means that when I convert my structs to bytes, the byte order for my values are reversed.

Is there a fairly straight-forward approach to either forcing my structs to contain data in Big Endian format, or is there a way to generically write these structs to byte arrays (and byte arrays to structs) that output Big Endian data?

Here is some sample code for what I've already done.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct StructType_1
{
    short shortVal;
    ulong ulongVal;
    int intVal;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct StructType_2
{
    long longVal_1;
    long longVal_2;
    long longVal;
    int intVal;
}

...

public static class StructHelper
{
    //How can I change the following methods so that the struct data 
    //is converted to and from BigEndian data?
    public static byte[] StructToByteArray<T>(T structVal) where T : struct
    {
        int size = Marshal.SizeOf(structVal);
        byte[] arr = new byte[size];
        IntPtr ptr = Marshal.AllocHGlobal(size);
        Marshal.StructureToPtr(structVal, ptr, true);
        Marshal.Copy(ptr, arr, 0, size);
        Marshal.FreeHGlobal(ptr);
        return arr;
    }

    public static T StructFromByteArray<T>(byte[] bytes) where T : struct
    {
        int sz = Marshal.SizeOf(typeof(T));
        IntPtr buff = Marshal.AllocHGlobal(sz);
        Marshal.Copy(bytes, 0, buff, sz);
        T ret = (T)Marshal.PtrToStructure(buff, typeof(T));
        Marshal.FreeHGlobal(buff);
        return ret;
    }
}
Matthew Watson

If you don't mind reading and writing each field to a stream (which may have performance implications) you could use Jon Skeet's EndianBinaryWriter: https://stackoverflow.com/a/1540269/106159

The code would look something like this:

public unsafe struct StructType_2
{
    long longVal_1;
    long longVal_2;
    long longVal;
    int intVal;
}

using (MemoryStream memory = new MemoryStream())
{
    using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, stream))
    {
        writer.Write(longVal_1);
        writer.Write(longVal_2);
        writer.Write(longVal);
        writer.Write(intVal);
    }

    byte[] buffer = memory.ToArray();

    // Use buffer
}

You would use the EndianBinaryReader for data going in the opposite direction.

This does of course have two fairly large drawbacks:

  • It's fiddly writing code to read and write each field.
  • It may be too slow to do it this way, depending on performance requirements.

Also have a look at this answers to this similar question: Marshalling a big-endian byte collection into a struct in order to pull out values

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Little Endian与Big Endian架构

来自分类Dev

为什么struct中的第一个打包数据是little endian,而其余的是big endian?

来自分类Dev

检查系统为Little Endian或Big Endian

来自分类Dev

Shortest way to write immutable struct in C#

来自分类Dev

将C中的char从big endian转换为little endian

来自分类Dev

Java中的Little Endian和Big Endian(Android)

来自分类Dev

.Net Core中的Big-Endian处理

来自分类Dev

怎么把UUID转换成big endian?

来自分类Dev

需要一个shell脚本将big endian转换为little endian

来自分类Dev

PHP-将Little Endian Hex转换为Big Endian Hex

来自分类Dev

数组值如何存储在Little Endian与Big Endian体系结构中

来自分类Dev

Little Endian vs Big Endian数据结构会改变我的答案吗?

来自分类Dev

在ARM中将16位big-endian快速转换为little-endian

来自分类Dev

在Big Endian和Little Endian机器中解释32位整数时的混乱

来自分类Dev

如何将boost :: multiprecision :: cpp_int从big endian更改为little endian

来自分类Dev

在Intel上使用ByteBuffers时,我应何时选择Little Endian与Big Endian?

来自分类Dev

将Big Endian顺序的32位变量转换为Little Endian

来自分类Dev

为什么python的结构认为little-endian和big-endian暗示了不同的长度?

来自分类Dev

示例中big-endian和little-endian处理器之间的区别

来自分类Dev

在Big Endian和Little Endian机器中解释32位整数时的困惑

来自分类Dev

将 little Endian hexdump 输出转换为 Big Endian(C 编程)的问题

来自分类Dev

Big-endian 或 Little-endian 对结构成员有何影响?

来自分类Dev

Numpy读取big-endian数据:数据类型格式

来自分类Dev

Java Byte []使用<<怪异值转换为int(Big Endian)

来自分类Dev

在Node JS上将数字转换为Big Endian

来自分类Dev

将int转换为char数组Big Endian

来自分类Dev

Java Byte []转换为使用<< Weirdness的int(Big Endian)

来自分类Dev

Numpy读取big-endian数据:数据类型格式

来自分类Dev

在Python中获取整数的big-endian字节序列

Related 相关文章

  1. 1

    Little Endian与Big Endian架构

  2. 2

    为什么struct中的第一个打包数据是little endian,而其余的是big endian?

  3. 3

    检查系统为Little Endian或Big Endian

  4. 4

    Shortest way to write immutable struct in C#

  5. 5

    将C中的char从big endian转换为little endian

  6. 6

    Java中的Little Endian和Big Endian(Android)

  7. 7

    .Net Core中的Big-Endian处理

  8. 8

    怎么把UUID转换成big endian?

  9. 9

    需要一个shell脚本将big endian转换为little endian

  10. 10

    PHP-将Little Endian Hex转换为Big Endian Hex

  11. 11

    数组值如何存储在Little Endian与Big Endian体系结构中

  12. 12

    Little Endian vs Big Endian数据结构会改变我的答案吗?

  13. 13

    在ARM中将16位big-endian快速转换为little-endian

  14. 14

    在Big Endian和Little Endian机器中解释32位整数时的混乱

  15. 15

    如何将boost :: multiprecision :: cpp_int从big endian更改为little endian

  16. 16

    在Intel上使用ByteBuffers时,我应何时选择Little Endian与Big Endian?

  17. 17

    将Big Endian顺序的32位变量转换为Little Endian

  18. 18

    为什么python的结构认为little-endian和big-endian暗示了不同的长度?

  19. 19

    示例中big-endian和little-endian处理器之间的区别

  20. 20

    在Big Endian和Little Endian机器中解释32位整数时的困惑

  21. 21

    将 little Endian hexdump 输出转换为 Big Endian(C 编程)的问题

  22. 22

    Big-endian 或 Little-endian 对结构成员有何影响?

  23. 23

    Numpy读取big-endian数据:数据类型格式

  24. 24

    Java Byte []使用<<怪异值转换为int(Big Endian)

  25. 25

    在Node JS上将数字转换为Big Endian

  26. 26

    将int转换为char数组Big Endian

  27. 27

    Java Byte []转换为使用<< Weirdness的int(Big Endian)

  28. 28

    Numpy读取big-endian数据:数据类型格式

  29. 29

    在Python中获取整数的big-endian字节序列

热门标签

归档