【发布时间】:2016-10-30 14:27:21
【问题描述】:
是否有正确的方法以大端格式递增 uint8 字符串? 例如我有:
uint8 test[] = {0,0,0,1};
test[3]++; //works
但是以这种方式进行类型转换的增量是否也是可能的?
*test = (uint32) *test+1; //doesnt work... Only the first test[0] will be incremented...
谢谢。
【问题讨论】:
是否有正确的方法以大端格式递增 uint8 字符串? 例如我有:
uint8 test[] = {0,0,0,1};
test[3]++; //works
但是以这种方式进行类型转换的增量是否也是可能的?
*test = (uint32) *test+1; //doesnt work... Only the first test[0] will be incremented...
谢谢。
【问题讨论】:
这个怎么样:
((uint32_t*)test)++;
但这仍然以本机字节顺序运行。
如果你有一个网络顺序的缓冲区,你应该这样做:
uint32_t *p = (uint32_t*)test;
uint32_t temp = ntohl(*p);
temp++;
*p = htonl(temp);
【讨论】: