随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

C#中byte[] 转 double[] 或 int[] 或 struct结构体

方法:使用C#调用C++ memcpy实现各种参数类型的内存拷贝

using System.Runtime.InteropServices;

 public class GlbWSGridDataset

{

   [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
        public static extern void MemCopy(int[] dest, byte[] src, int count);//字节数组到整形数组的拷贝

        [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
        public static extern void MemCopy(double[] dest, byte[] src, int count);//字节数组到double数组的拷贝

        [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
        public unsafe static extern void MemCopy(ref GlbcwSGridFacet dest, byte[] src, int count);//注意只有结构体能这么做,class不可以

  public void test()

  {    

    string filepath = @"c:\ddd.sgr";
              FileStream fs = new FileStream(filepath, FileMode.Open);//初始化文件流
              BinaryReader binReader = new BinaryReader(fs);

    {///byte[] 转 double[]

      int zcount = (grid.ni) * (grid.nj) * (grid.nk + 1);
      double[] zArray = new double[zcount * 4];
      System.Diagnostics.Trace.Assert(zArray != null);
      int zlen = zcount * 4 * sizeof(double);
      byte[] zdata = new byte[zlen];
      zdata = binReader.ReadBytes(zlen);

      // 调用c++的memcopy实现拷贝
      MemCopy(zArray, zdata, zlen);

    }

    {///byte[] 转 int[]

      int clrcount = (NI) * (NJ) * (NK);
                clrArray = new int[clrcount];
                System.Diagnostics.Trace.Assert(clrArray != null);
                int len = clrcount * sizeof(int);
                byte[] clrdata = new byte[len];
                clrdata = binReader.ReadBytes(len);
                // 拷贝整型数组
                MemCopy(clrArray, clrdata, len);

    }

    

    {///byte[] 转struct

      GlbcwSGridFacet bounderCellIndexs = new GlbcwSGridFacet();

      // 使用Marshal.SizeOf来计算struct的大小

      int len = System.Runtime.InteropServices.Marshal.SizeOf(typeof(GlbcwSGridFacet))

      byte[] data= new byte[len];
                System.Diagnostics.Trace.Assert(data!= null);
                data= binReader.ReadBytes(len);
                // 用不安全的拷贝方式memcpy  效率最快
                MemCopy(ref bounderCellIndexs, data, len);   

    }

       // 关闭文件   

              binReader.Close();
              fs.Close();

 

  }

}

参考 https://www.cnblogs.com/timeObjserver/p/7040966.html?utm_source=itdadao&utm_medium=referral  C#调用C++ memcpy实现各种参数类型的内存拷贝 VS marshal.copy的实现 效率对比

 

posted on   3D入魔  阅读(1733)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2016-06-28 用CHttpFile实现简单的GET/POST数据【转】
2007-06-28 64位进程调用32位dll的解决方法
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示