C# byte[]、struct、intptr、byte[]和byte*等的相互转换

struct、byte[]互相转换

cs 复制代码
//struct转换为byte[]
public static byte[] StructToBytes(object structObj)
{
    int size = Marshal.SizeOf(structObj);
    IntPtr buffer = Marshal.AllocHGlobal(size);
    try
    {
        Marshal.StructureToPtr(structObj, buffer, false);
        byte[] bytes = new byte[size];
        Marshal.Copy(buffer, bytes, 0, size);
        return bytes;
    }
    finally
    {
        Marshal.FreeHGlobal(buffer);
    }
}

//byte[]转换为struct
public static object BytesToStruct(byte[] bytes, Type type)
{
    int size = Marshal.SizeOf(type);
    IntPtr buffer = Marshal.AllocHGlobal(size);
    try
    {
        Marshal.Copy(bytes, 0, buffer, size);
        return Marshal.PtrToStructure(buffer, type);
    }
    finally
    {
        Marshal.FreeHGlobal(buffer);
    }
}

Intptr、byte[]互相转换

cs 复制代码
//IntPtr转byte[] 
IntPtr y=new IntPtr();
byte[] ys = new byte[yLength];
Marshal.Copy(y, ys, 0, yLength);

//IntPtr转byte[]
private byte[] IntPtrToByte(IntPtr unmanagedMemory, int dataSize)
{
    byte[] managedArray = new byte[dataSize];
   
    try
    {
        // 复制托管数组的内容到非托管内存
        Marshal.Copy(managedArray, 0, unmanagedMemory, managedArray.Length);
    }
    catch (Exception ex)
    {
        // 处理任何异常,比如内存不足等
        Console.WriteLine($"Error: {ex.Message}");
        return managedArray;
    }
    return managedArray;
}


//byte[]转换为Intptr
public static Intptr BytesToIntptr(byte[] bytes)
{
    int size = bytes.Length;
    IntPtr buffer = Marshal.AllocHGlobal(size);
    try
    {
        Marshal.Copy(bytes, 0, buffer, size);
        return buffer;
    }
    finally
    {
        Marshal.FreeHGlobal(buffer);
    }
}

//输入byte[],返回IntPtr ,不使用Marshal创建新的堆,节省内存开销,也避免忘记释放导致的问题
private IntPtr ArrToPtr(byte[] array)
{
   return System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
}

byte[]、byte*互相转换

cs 复制代码
//从byte*转byte[]
byte[] barr = new byte[10];
byte* bp = (byte*)Marshal.AllocHGlobal(10);
for (int i = 0; i < 10; i++)
    bp[i] = (byte)i;
Marshal.Copy((IntPtr)bp, barr, 0, 10);
PrintArray(barr);
Marshal.FreeHGlobal((IntPtr)bp);


//从byte[]到byte*
byte[] barr = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
byte* bp = (byte*)Marshal.AllocHGlobal(5);
PrintPtr(bp, 5);
Marshal.Copy(barr, 3, (IntPtr)bp, 5);
PrintPtr(bp, 5);
Marshal.FreeHGlobal((IntPtr)bp);

static void PrintPtr(byte* bp,int n)
{
    for(int i=0;i<n;++i)
        Console.Write(bp[i] + " ");
    Console.WriteLine();
}

其他请参考

https://www.cnblogs.com/jhlong/p/5715015.html

https://www.cnblogs.com/castor-xu/p/14719493.html

相关推荐
小码编匠11 分钟前
WPF 中的高级交互通过右键拖动实现图像灵活缩放
后端·c#·.net
唐青枫7 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez201013 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务3 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther3 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎