序列化和反序列化uint64_t数据

时间:2022-05-03
本文章向大家介绍序列化和反序列化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;
}