1. Byte [] Conversion is performed in bytes
Use the BitConverter class for the conversion.
//int a =1; Convert to 4-byte byte[] = new byte[4];
int a = 1;
byte[] rets = BitConverter.GetBytes(a);
Console(rets.Length); // The result is 4
Copy the code
API documentation: BitConverter class (System) | Microsoft Docs
2. Convert string to byte[]
The API documentation: Encoding (System. Text) | Microsoft Docs
3. Array Copy between arrays
// Here we concatenate agreement, childAgreement, bytes into a content
/ / agreement 4 bytes
/ / childAgreement 4 bytes
/ / bytes 4 bytes
/ / the content of 12 bytes
byte[] agreement = BitConverter.GetBytes(1); //CAXADNC Siemens data protocol
byte[] childAgreement = BitConverter.GetBytes(1); // Siemens Data Protocol Version 1
byte[] bytes = BitConverter.GetBytes(7); / / command
byte[] content = new byte[12]; // The entire command content
Array.Copy(agreement, 0, content, 0.4);
Array.Copy(childAgreement, 0, content, 4.4);
Array.Copy(bytes, 0, content, 7.4);
Copy the code
The API documentation: Array. (the System) | Microsoft Docs Copy method
4. Conversion between Struct and byte[]
Reference: byte alignment, conversion operations and copying to binary data for structs in C# (byte[])
5. Obtain Struct structure size
Use Marshal.SizeOf Method to get the number of bytes of Struct.
The API documentation:
Marshal.SizeOf Method
The use of MarshalAs
Marshalling .NET generic types
When communicating with structs of other languages:
C# call C++DLL pass structure array ultimate solution _ learn and learn -CSDN blog
C# receive C/C++DLL structure nested array and multidimensional array solution _ learn and learn -CSDN blog
A simple summary of MarshalAs _ Iron Man -CSDN blog
Classic:
Use of MarshalAs _ development is zigzag but also forward -CSDN blog
Use the Signature Tool to automatically generate P/Invoke to call Windows API C# function declaration _Donjuan column -CSDN blog
A good tool to recommend :P/Invoke Interop Assistant
c# – Easiest way to generate P/Invoke code? – Stack Overflow
Download address:
PInvokeInteropAssistant.zip – making blue cloud (lanzoui.com)
6. Byte [] and hexadecimal character string
Refer to blog post:
C# byte arrays convert hexadecimal strings to each other
C# and c++ type comparison table
C++ | C# | ||
---|---|---|---|
WORD | ushort | ||
DWORD | uint | ||
UCHAR | Int /byte(you can use int instead in most cases, but use byte if strict alignment is required) | ||
UCHAR* | string/IntPtr | ||
unsigned char * | [MarshalAs (UnmanagedType. LPArray)] byte [] /? (Intptr) | ||
char* | string | ||
LPCTSTR | string | ||
LPTSTR | [MarshalAs(UnmanagedType.LPTStr)] string | ||
long | int | ||
ulong | uint | ||
Handle | IntPtr | ||
HWND | IntPtr | ||
void * | IntPtr | ||
int | int | ||
int* | ref int | ||
*int | IntPtr | ||
unsigned int | uint | ||
COLORREF | uint |
Reference:
C# and C++ data type comparison table – kuiblog.com
C++ and C# data structure type mapping table – sunrack – cnblogs.com
C/C # data type mapping – Lazyman – Cnblogs.com