【问题标题】:C: Correct Increment of uint8 big endian stringC:uint8大端字符串的正确增量
【发布时间】: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... 

谢谢。

【问题讨论】:

    标签: string increment uint32


    【解决方案1】:

    这个怎么样:

    ((uint32_t*)test)++;
    

    但这仍然以本机字节顺序运行。

    如果你有一个网络顺序的缓冲区,你应该这样做:

    uint32_t *p = (uint32_t*)test;
    uint32_t temp = ntohl(*p);
    temp++;
    *p = htonl(temp);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 2013-12-13
      • 2013-02-18
      • 2013-04-25
      • 1970-01-01
      • 2021-03-12
      • 2019-07-28
      相关资源
      最近更新 更多