gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:34:49 24 4
gpt4 key购买 nike

我有一个函数想要接收 sbyte* 缓冲区如何在 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
}

关于c# - 如何在 C# 中将 byte[] 转换为 sbyte*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3543539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com