gpt4 book ai didi

c - 如何将 `SInt16' 转换为 `double'?

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:13 25 4
gpt4 key购买 nike

如何将 SInt16 转换为 double 或将 Float32 转换为 double?我知道我可以像这样将 SInt16 转换为 Float32:

double *buffer;
SInt16* frames;

for (i = 0; i < number_of_frames; i++) {
Float32 currentFrame = frames[i] / 32768.0f;
}

但我正在使用许多 C 函数,我想在 double 中将数据传递给它们。

现在我使用这段代码:

for (i = 0; i < number_of_frames; i++) {

Float32 currentFrame = frames[i] / 32768.0f;

buffer[i] = currentFrame;
}

但我不确定结果是否正确。

最佳答案

原始 C 类型(整数、字符、浮点类型)可以通过强制转换进行相互转换。因此,给定一个有符号整数值 my_signed_integer,您可以通过简单的转换将其转换为 double:

((double) my_signed_integer)

我猜你的 Sint16Float32 是 typedef,所以你仍然可以做同样的事情:

((double) my_SInt16_value) /* Just use this expression as a double
because it IS a double. */

最终,这意味着:

for (i=0; i<number_of_frames; i++ )
{
buffer[i] = ((double) frames[i]) / 32768.0;
}

实际上,您不需要此转换。 frames[i] 将自动转换为 double 因为 implicit conversion .

关于c - 如何将 `SInt16' 转换为 `double'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428056/

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