在C ++中增加16字节字符数组

时间:2016-03-17 23:34:23

标签: c++ c

我有一个数组

  

char msgID [16];

如何将此增加1?我将高低8字节读入2个不同的uint64_t整数

  

uint64_t高,低;

     

memcpy(& low,msgID,sizeof(uint64_t));

     

memcpy(& high,msgID + sizeof(uint64_t),sizeof(uint64_t));

如果我这样做

  

低+ = 1; //我如何解释溢出?

感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:5)

实际上很简单:

if(++low == 0)
    ++high;

答案 1 :(得分:1)

msgID[15]开始并增加。如果它通过255,它会回到零,增加前一个字节

for (int c = 15; c >= 0; c--)
    if (++msgID[c] != 0) 
            break;

根据建议的评论,做结束测试并相应增加

int endian_test = 1;
unsigned char buf[4];
memcpy(buf, &endian_test, 4);
//buf will be "1000" for little-endian, and "0001" for big-endian
int is_little_endian = buf[0];

if (is_little_endian)
{
    for (int c = 0; c < 16; c++)
        if (++msgID[c] != 0)
            break;
}
else
{
    for (int c = 15; c >= 0; c--)
        if (++msgID[c] != 0)
            break;
}