C# Delphi与c语言的转换#
我真的需要您的帮助,将此c#代码移植到Delphi one:C# Delphi与c语言的转换#,c#,delphi,C#,Delphi,我真的需要您的帮助,将此c#代码移植到Delphi one: public unsafe byte[] Encode(byte[] inputPcmSamples, int sampleLength, out int encodedLength) { if (disposed) throw new ObjectDisposedException("OpusEncoder"); int frames
public unsafe byte[] Encode(byte[] inputPcmSamples, int sampleLength, out int encodedLength)
{
if (disposed)
throw new ObjectDisposedException("OpusEncoder");
int frames = FrameCount(inputPcmSamples);
IntPtr encodedPtr;
byte[] encoded =new byte [MaxDataBytes];
int length = 0;
/* How this can be ported to delphi */
fixed (byte* benc = encoded)
{
encodedPtr = new IntPtr((void*)benc);
length = API.opus_encode(_encoder, inputPcmSamples, frames, encodedPtr, sampleLength);
}
encodedLength = length;
if (length < 0)
throw new Exception("Encoding failed - " + ((Errors)length).ToString());
return encoded;
}
非常感谢您似乎想知道如何处理C#中的
fixed
块
使用fixed
可以锁定托管数组,以获取要传递到非托管代码的指针
在Delphi中,我们只需要一个字节数组和指向该数组的指针。看起来是这样的:
或许:
length := opus_encode(..., PByte(encoded), ...);
length := opus_encode(..., @encoded[0], ...);
或许:
length := opus_encode(..., PByte(encoded), ...);
length := opus_encode(..., @encoded[0], ...);
取决于您如何声明导入的函数opus\u encode
和您的首选项
如果
MaxDataBytes
是一个足够小的值,缓冲区可以存在于堆栈上,并且MaxDataBytes
在编译时是已知的,那么可以使用固定长度数组。那么您的问题是什么?@David Heffernan非常感谢,我的问题是如何将该部分移植到delphi中。您正在努力解决的是哪一部分?你懂C代码吗?你知道API.opus_encode的背后是什么吗?因为显然我们都不知道?请更加努力地提供上下文。请清楚说明您迄今为止尝试了什么,以及失败的原因。fixed(byte*benc=encoded)
pinsencoded
获取其地址。在Delphi中不需要钉住<代码>编码DPTR=新的IntPtr((无效*)基准)几乎是禁止操作的。它将字节*
强制转换为void*
,然后转换为IntPtr
。您可能正在寻找Delphi指针
类型。所以我相信你只是在寻找类似encodedPtr:=@encoded
(免责声明:我已经好几年没有接触Delphi了)@David Heffernan:是的,我知道“API.opus_encode”的背后是什么,只是知道如何将它移植到Delphi:fixed(byte*benc=encoded){encodedPtr=new IntPtr((void*)benc);
length := opus_encode(..., @encoded[0], ...);