zoukankan      html  css  js  c++  java
  • 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的实现 效率对比

  • 相关阅读:
    IOS使用正则表达式去掉html中的标签元素,获得纯文本
    iOS 拨打电话的三种方式总结
    iOS中Block的基础用法
    如何避免在Block里用self造成循环引用
    对MAC自带的SVN进行升级
    IOS开发之记录用户登陆状态
    Xcode7 添加PCH文件
    mysql upgrade
    Ubuntu下更改用户名和主机名
    mysql 查询的时候没有区分大小写的解决方案
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/9237661.html
Copyright © 2011-2022 走看看