以 C++ 为单位递增 16 字节字符数组
Increment 16 byte char array in C++
我有一个数组
char msgID[16];
如何将其递增 1?我将高低 8 字节读入 2 个不同的uint64_t
整数
uint64_t high, low;
memcpy(&low, msgID, sizeof(uint64_t));
memcpy(&high, msgID + sizeof(uint64_t) , sizeof(uint64_t));
如果我这样做
low += 1;
如何解释溢出?
感谢您提供的任何帮助。
实际上,这很简单:
if(++low == 0)
++high;
从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;
}
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 多字节字符'377777'如何工作?
- 文件名如何在"char"和2字节字符之间工作
- 在 linux/mac 中获取多字节字符的"char"
- 如何编写完全可移植的 4 字节字符常量的编译时初始化
- 操作多字节字符的字符串
- Visual Studio 多字节字符到单个字节
- 以 C++ 为单位递增 16 字节字符数组
- setw() 在包含 UTF-8 多字节字符/码位的字符串上输入错误的输出
- C++子字符串多字节字符
- Windows 中的 4 字节字符字符串
- 创建未知长度的字节/字符数组
- 如何提取tar.gz,包括多字节字符和windows上禁止的字符
- Unicode字符或多字节字符:哪种类型更有效?为什么
- 为具有1字节和2字节字符的字符集实现退格
- c++如何获取下一个多字节字符
- 通过boost::asio传输混合unicode和单字节字符的最佳方式
- 如何在 2 字节字符中存储 1 个数字
- C++返回字符串中最常见字符的函数中的错误.多字节字符
- 在MBCS操作系统上运行单字节字符应用程序的风险