如何在C#中将byte []转换为sbyte *?

时间:2010-08-22 21:40:58

标签: c# .net visual-c++ byte

我有一个想要接收sbyte *缓冲区的函数如何在C#中从头开始和从现有的byte []创建这样的函数?

2 个答案:

答案 0 :(得分:6)

// 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
}

答案 1 :(得分:3)

转换为Array然后转换为byte []就足够了。

byte [] unsigned = {0,1,2};

sbyte [] signed =(sbyte [])(Array)unsigned;