序列化和反序列化uint64_t数据

 void serializeu64(unsigned long long i,char buf[])
{
  unsigned long long mask = 0xff00000000000000;//字节掩码位
  for(int l = 0;l<8;l++)
  {
    auto move = 8-l-1;
    auto f = i&mask;//取对应字节掩码位的字节数据
    char res = (char)(f>>(8*move));
    buf[l]=res;
    mask = mask >> 8;
  }
}
 
unsigned long long deserializeu64(char buf[])
{
  unsigned long long i=0;
  for(int l = 0;l<8;l++)
  {
    auto move = 8 -l -1;
    unsigned char res =buf[l];
    unsigned long long ress = (unsigned long long)res;
    i += (unsigned long long)(ress<<(move*8));
  }
  return i;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫描关注云+社区