【发布时间】:2011-06-13 08:32:31
【问题描述】:
将“无符号字符”数组转换为“无符号短”数组的有效方法是什么?我通常使用下面的代码sn -p来做。
#define CH_LINE_PIXELS 2291
#define SCANLINE_SIZE 57301
#define CH1_INDEX 2297
#define CH2_INDEX 4592
#define CH3_INDEX 6887
#define CH4_INDEX 9182
unsigned char* pUChar = new unsigned char[SCANLINE_SIZE];
unsigned short *pUS1, *pUS2, *pUS3, *pUS4;
pUS1 = reinterpret_cast<unsigned short *>(&pUChar[CH1_INDEX]);
pUS2 = reinterpret_cast<unsigned short *>(&pUChar[CH2_INDEX]);
pUS3 = reinterpret_cast<unsigned short *>(&pUChar[CH3_INDEX]);
pUS4 = reinterpret_cast<unsigned short *>(&pUChar[CH4_INDEX]);
unsigned short us1, us2;
for (unsigned int i = 0; i < CH_LINE_PIXELS; i++)
{
us1 = pUChar[CH1_INDEX + 2 * i];
us2 = pUChar[CH1_INDEX + 2 * x + 1];
pUS1[x] = us1 * 0x100 + us2;
us1 = pUChar[CH2_INDEX + 2 * i];
us2 = pUChar[CH2_INDEX + 2 * i + 1];
pUS2[x] = us1 * 0x100 + us2;
us1 = pUChar[CH3_INDEX + 2 * i];
us2 = pUChar[CH3_INDEX + 2 * i + 1];
pUS3[x] = us1 * 0x100 + us2;
us1 = pUChar[CH4_INDEX + 2 * i];
us2 = pUChar[CH4_INDEX + 2 * i + 1];
pUS4[x] = us1 * 0x100 + us2;
}
【问题讨论】:
-
我看到您忘记阅读标题为“如何格式化”的巨大侧边栏。请注意,它没有说明使用 HTML 标记...
-
具体来说,它说*只需选择所有代码并按下
{}按钮。请在以后这样做。 -
请注意,以这种方式使用
reinterpret_cast是依赖于实现:也就是说,您不能期望您的代码在不同的编译器/平台上编译时的行为方式相同。 -
亲爱的jalf 感谢您对代码格式的好意提醒,我以后会这样做。
-
亲爱的ereOn,您对删除编译器/平台依赖的“cast”操作有何建议?