当前位置:网站首页>C byte array and class mutual conversion
C byte array and class mutual conversion
2022-07-24 01:29:00 【Wu Zimu】
When doing serial communication , Encountered a very interesting way of writing
The serial port information here is not a string , But a class represented by a byte array
Note that this is not json character string , It's an array of bytes
The idea is to open up a piece of memory in memory , Insert byte array into memory , Then encapsulate this memory as an object of the corresponding class
The code is as follows
Need to quote
using System.Runtime.InteropServices;
/// <summary>
/// Byte to byte RtuAck Structure
/// </summary>
/// <param name="arr"></param>
public RtuAck Bytes2Ack(byte[] arr)
{
RtuAck ack = new RtuAck();
int size = Marshal.SizeOf(ack);
IntPtr ptr = Marshal.AllocHGlobal(size); // Declare an unmanaged memory , To hold arr
Marshal.Copy(arr, 0, ptr, size);// take arr Stored in the declared memory
ack = (RtuAck)Marshal.PtrToStructure(ptr, ack.GetType());// Turn this memory into ack type
Marshal.FreeHGlobal(ptr);// Release the unmanaged memory just declared
return ack;
}
conversely , The code for transferring from class object to byte array is as follows
public byte[] Ack2Bytes(RtuAck ack)
{
int size = Marshal.SizeOf(ack);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(ack, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
边栏推荐
- Win11 highlights of win11 system
- Understanding of flexible array in C language
- Network type (notes on the third day)
- HCIP第十一天笔记
- HCIP第五天笔记
- SCM learning notes 9 -- Serial Communication (based on Baiwen STM32F103 series tutorials)
- HCIP第八天笔记
- 刚开始使用,请教些问题和教程或分享帖子
- 复现一句话木马
- Answer to the short answer question of polymer synthesis technology
猜你喜欢

Data warehouse construction - ods floor

HCIP第二天笔记

面试题之:ArrayList和LinkedList有哪些区别

HCIP第四天笔记

Easyexcel export case (only you can't think of it)

Hcip, OSPF comprehensive experiment

Arm architecture and programming 5 -- GCC and makefile (based on Baiwen arm architecture and programming tutorial video)

Hcip seventh day notes

Arm architecture and programming 6 -- Relocation (based on Baiwen arm architecture and programming tutorial video)

Navicat for MySQL installation tutorial
随机推荐
代码阅读方法与最佳实践
Research on retinal vascular segmentation based on GAN using few samples
MGRE experiment
OSPF(第五天笔记)
2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元
基于强化空间注意力的视网膜网络(ESA-Unet)
Navicat for MySQL installation tutorial
HCIP第二天笔记
数字签名技术简介
Domestic MCU and SOC are rising, but they are still lagging behind in special fields
High voltage technology learning summary
How the next dbcontext of efcore advanced SaaS system supports multi database migration
Hcip day 8 notes
Arm architecture and programming 6 -- Relocation (based on Baiwen arm architecture and programming tutorial video)
High voltage technology test questions and answers
Arm architecture and programming 5 -- GCC and makefile (based on Baiwen arm architecture and programming tutorial video)
Collation of key knowledge of high voltage technology
Repeat one sentence Trojan horse
Simple Gan instance code
数仓搭建——ODS层