zoukankan      html  css  js  c++  java
  • byte struct 互转

            public static T ByteToStructure<T>(byte[] dataBuffer)
            {
                object structure = null;
                int size = Marshal.SizeOf(typeof(T));
                IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
                try
                {
                    Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
                    structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
                }
                finally
                {
                    Marshal.FreeHGlobal(allocIntPtr);
                }
                return (T)structure;
            }
            public static byte[] StructureToByte<T>(T structure)
            {
                int size = Marshal.SizeOf(typeof(T));
                byte[] buffer = new byte[size];
                IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
                try
                {
                    Marshal.StructureToPtr(structure, bufferIntPtr, true);
                    Marshal.Copy(bufferIntPtr, buffer, 0, size);
                }
                finally
                {
                    Marshal.FreeHGlobal(bufferIntPtr);
                }
                return buffer;
            }
  • 相关阅读:
    cJson
    STemWin
    TEA通讯加密
    stm32串口收发导致的死机
    C语言版数据结构算法
    FIFO
    IAP远程在线升级
    LWIP
    电能计量芯片
    单片机里的堆栈
  • 原文地址:https://www.cnblogs.com/ahuo/p/2984225.html
Copyright © 2011-2022 走看看