Tag: memcpy

在C#中通过TCP发送C结构

我正在编写一个程序,通过TCP与一个设备的管理界面进行交互。 问题是,设备的文档是用C语言编写的,而我写的程序是用C#编写的。 我的问题是,文档指定 通信基于基于C结构的API缓冲区 没有任何谷歌搜索似乎可以指向我这个API或我如何通过TCP发送原始结构。 文档似乎暗示我应该使用memcpy将结构复制到TCP缓冲区,但C#不直接支持memcpy。 在C#中是否存在等效方法或以不同方式实现此目的

C#中的快速memcpy

我想用这样的原型编写一个C#方法: void memcpy(byte[] dst, int dstOffset, byte[] src, int srcOffset, int len); 我有这个方法的2个选项: 1。 void memcpy(byte[] dst, int dstOffset, byte[] src, int srcOffset, int len) { for (int i = 0; i < len; i++) { dst[dstOffset + i] = src[srcOffset + i]; } } 2。 void memcpy(byte[] dst, int dstOffset, byte[] src, int srcOffset, […]

将字节数组复制到C#中的类/结构中的各个字段

在下面的示例C#代码中,我有一个从套接字读取的字节数组。 我想将数据解析为’exampleClass’的各个字段(前8个字节到64位变量’field1’,接下来4个字节到32位变量’field2’等) using System; namespace CsByteCopy { class Program { class ExampleClass { public UInt64 field1; public UInt32 field2; public UInt16 field3; public byte[] field4 = new byte[18]; } static void Main(string[] args) { byte[] exampleData = { // These 8 bytes should go in ‘field1’ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, // These 4 bytes should go in ‘field2’ […]