当前位置:网站首页>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;
}
边栏推荐
- HCIP实验
- Hcip day 4 notes
- B tree and b+ tree
- 2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元
- Detailed explanation of OSPF in hcip
- EFCore高级Saas系统下一个DbContext如何支持多数据库迁移
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- SCM learning notes 8 -- keys and external interrupts (based on Baiwen STM32F103 series tutorials)
- Summary of HCIA knowledge points
- High voltage technical examination questions with answers
猜你喜欢

Arm architecture and programming 1 -- LED flashing (based on Baiwen arm architecture and programming tutorial video)

2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790

HCIP第七天笔记

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

Idea compiler sets the separation line between methods

Hcip, OSPF comprehensive experiment

复现一句话木马

HCIP网络类型,ppp会话,数据链路层协议

HCIP实验

HCIP第二天笔记
随机推荐
Simple Gan instance code
Concept, key points and summary of postgraduate entrance examination in Polymer Physics
The first day of little bear pie
Win11 highlights of win11 system
win11系统之win11亮点
SCM learning notes 4--gpio (based on Baiwen STM32F103 series tutorials)
Domestic MCU and SOC are rising, but they are still lagging behind in special fields
数字化转型时代的企业数据新基建 | 爱分析报告
General method of C language supporting yaml configuration file
B tree and b+ tree
Parsing yaml configuration files using C language and libcyaml Library
Leetcode -- 136. a number that appears only once
2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790
Redis - configuration and Application
Hcip day 9 notes
vantUI,Axiso,常见问题及使用:
Introduction and environment construction of little bear sect
Hcip day 8 notes
HCIP第八天笔记
After the interview with 20 or 30 companies, there is no offer that you can't get after the Android interview