[Help]方法在C ++中将short数组连接到unsigned long [英] [Help] method to concatenate shorts array to unsigned long in C++
问题描述
Hello Everyone,
我想连接成一个unsigned long以下的short数组,并能够解析它以重建short数组。
我怎么能缩小它并避免溢出?
非常感谢你提前。
祝你好运。
MiQi
我的尝试:
以下是我到目前为止:
短 exp [ 10 ];
exp [ 0 ] = 1 ; exp [ 1 ] = 0 ; exp [ 2 ] = - 2 ; exp [ 3 ] = 0 ; exp [ 4 ] = - 1 ; exp [ 5 ] = 0 ; exp [ 6 ] = 2 ; exp [ 7 ] = 0 ; exp [ 8 ] = 0 ; exp [ 9 ] = 1 ;
for ( int index = 0 ; index< sizeof (exp)/ sizeof ( short ); index ++)
{
if (index% 2 == 0 )
exp [index] = 5 ; // 0101
else exp [index] = - 5 ; // 1101
}
LARGE_INTEGER uiCode;
uiCode.QuadPart = 0 ;
for (index = 0 ; index< sizeof (exp)/ sizeof ( short ); index ++)
{
uiCode.QuadPart = uiCode.QuadPart | (exp [index]& 0x000f);
uiCode.QuadPart = uiCode.QuadPart<< 4 ;
}
LARGE_INTEGER uiExpectedCode;
uiExpectedCode.QuadPart = uiCode.QuadPart / LONG_MAX;
LARGE_INTEGER uiResiduCode;
uiResiduCode.QuadPart = uiCode.QuadPart%LONG_MAX;
long SchrinkedCode = ??;
< blockquote>目前还不是很清楚你想要实现什么但是有一些可能的错误。
这里-5不是二进制1101:
else exp [index] = -5; // 1101
-5是十六进制0xFFFB。所以最低的4位是1011.
将半字节(4位值)转换为四边形部分的循环应该首先转移:
for(index = 0; index< sizeof(exp)/ sizeof(short); index ++)
{
uiCode.QuadPart<< = 4;
uiCode.QuadPart | =(exp [index]& 0x000f);
}
否则最低的四位将始终为零,高位可能会被移出。
最后一个提示。可以根据定义初始化数组:
short exp [10] = {1,0,-2,0,-1,0,2,0,0,1} ;
Hello Everyone,
I would like to concatenate into an unsigned long the following shorts array and be able to decompact it to rebuild the shorts array.
How could I schrink it and avoid overflow ?
Thank you very much in advance.
Best regards.
MiQi
What I have tried:
Here is what I have so far:
short exp[10];
exp[0] = 1; exp[1] = 0; exp[2] = -2; exp[3] = 0; exp[4] = -1; exp[5] = 0; exp[6] = 2; exp[7] = 0; exp[8] = 0; exp[9] = 1;
for (int index = 0; index < sizeof(exp) / sizeof(short); index++)
{
if (index % 2 == 0)
exp[index] = 5; // 0101
else exp[index] = -5;// 1101
}
LARGE_INTEGER uiCode;
uiCode.QuadPart = 0;
for (index = 0; index < sizeof(exp) / sizeof(short); index++)
{
uiCode.QuadPart = uiCode.QuadPart | (exp[index] & 0x000f);
uiCode.QuadPart = uiCode.QuadPart << 4;
}
LARGE_INTEGER uiExpectedCode;
uiExpectedCode.QuadPart = uiCode.QuadPart / LONG_MAX;
LARGE_INTEGER uiResiduCode;
uiResiduCode.QuadPart = uiCode.QuadPart % LONG_MAX;
long SchrinkedCode = ??;
It is not quite clear what you want to achieve but there are some possible errors.
Here -5 is not binary 1101:
else exp[index] = -5;// 1101
-5 is hex 0xFFFB. So the lowest 4 bits are 1011.
The loop to shift the nibbles (4 bit values) into the quad part should probably shift first:
for (index = 0; index < sizeof(exp) / sizeof(short); index++) { uiCode.QuadPart <<= 4; uiCode.QuadPart |= (exp[index] & 0x000f); }
Otherwise the lowest four bits will be always zero and the upper bits may get shifted out.
Finally a tip. Arrays can be initialised upon definition:
short exp[10] = { 1, 0, -2, 0, -1, 0, 2, 0, 0, 1 };
这篇关于[Help]方法在C ++中将short数组连接到unsigned long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!