簡體   English   中英

如何在C#中將byte []轉換為sbyte *?

[英]How to turn byte[] into sbyte* in C#?

我有一個函數想要接收sbyte * buffer如何在C#中從頭開始和從現有的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
}

轉換為Array然后轉換為byte []就足夠了。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM