如何将byte []的进为sbyte *在C#中? [英] How to turn byte[] into sbyte* in C#?

查看:1191
本文介绍了如何将byte []的进为sbyte *在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望接收为sbyte *缓冲区如何在C#从头开始创建这样的功能,并从现有的byte []?

解决方案

  //分配一个新的缓冲区(跳过此如果你已经有一个)
byte []的缓冲区=新的字节[256];
不安全
{
    //固定语句告诉运行时保持数组中相同的
    //发生在存储器(搬迁它将使指针无效)
    固定(字节* ptr_byte =安培;缓冲[0])
    {
        //把指针到sbyte *
        为sbyte * ptr_sbyte =(为sbyte *)ptr_byte;

        //这里做你的东西
    }

    //固定块的结束通知运行时,原来的数组
    //可用于搬迁和/或垃圾回收再
}
 

I have a function that want to receive sbyte* buffer how to create such in C# from scratch and from existing byte[]?

解决方案

// Allocate a new buffer (skip this if you already have one)
byte[] buffer = new byte[256];
unsafe
{
    // The "fixed" statement tells the runtime to keep the array in the same
    // place in memory (relocating it would make the pointer invalid)
    fixed (byte* ptr_byte = &buffer[0])
    {
        // Cast the pointer to sbyte*
        sbyte* ptr_sbyte = (sbyte*) ptr_byte;

        // Do your stuff here
    }

    // The end of the "fixed" block tells the runtime that the original array
    // is available for relocation and/or garbage collection again
}

这篇关于如何将byte []的进为sbyte *在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆