5
我有一個函數,想要接收sbyte *緩衝區如何在C#中從頭創建和從現有字節[]?如何在C#中將byte []轉換爲sbyte *?
我有一個函數,想要接收sbyte *緩衝區如何在C#中從頭創建和從現有字節[]?如何在C#中將byte []轉換爲sbyte *?
// 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 [] unsigned = {0,1,2};
sbyte [] signed =(sbyte [])(Array)unsigned;