gpt4 book ai didi

c# - 将 int[] 转换为 short[],反之亦然

转载 作者:太空宇宙 更新时间:2023-11-03 19:00:25 25 4
gpt4 key购买 nike

我正在尝试使用 Kinect for Xbox 360 和 SDK 1.8 计算玩家的表面积。我遇到了一个小问题,我需要将 short[] 转换为 int[],反之亦然。解释更多...

        short[] rawDepthData = new short[depthFrame.PixelDataLength];
depthFrame.CopyPixelDataTo(rawDepthData);

但我需要 int[] 格式的 rawDepthData..

感谢帮助非常感谢

最佳答案

对于您可能希望尽可能快的事情,您可能需要考虑编写自己的循环以将 short[] 数组复制到新的 int[] 数组,例如:

public static int[] Convert(short[] input)
{
int[] result = new int[input.Length];

for (int i = 0; i < input.Length; ++i)
result[i] = input[i];

return result;
}

这是您转换回来的方式。重要提示:这假设每个 int 值都适合一个 short。否则,它们将被截断并且值将发生变化!

public static short[] Convert(int[] input)
{
short[] result = new short[input.Length];

unchecked
{
for (int i = 0; i < input.Length; ++i)
result[i] = (short) input[i];
}

return result;
}

(注意:将此代码与 Array.ConvertAll() 的性能进行比较时,您必须将此代码编译为 RELEASE 版本 - 否则您会将调试版本与发布版本进行比较构建。)

关于c# - 将 int[] 转换为 short[],反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36842664/

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