How do I convert a 64bit integer to a char array and back?

前端 未结 4 1368
旧时难觅i
旧时难觅i 2020-12-08 23:20

I am having trouble converting a int64_t to a char array and back. I don\'t know what is wrong with the code below, it makes complete logical sense to me. The code works for

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 23:54

    This is rather simple, the problem is that you are shifting bits in the char array but size of a[i] is 4 byes (upcast to int), so your shift just goes over range. Try replacing this in your code:

    int64_t charTo64bitNum(char a[]) {
      int64_t n = 0;
      n = (((int64_t)a[0] << 56) & 0xFF00000000000000U)
        | (((int64_t)a[1] << 48) & 0x00FF000000000000U)
        | (((int64_t)a[2] << 40) & 0x0000FF0000000000U)
        | (((int64_t)a[3] << 32) & 0x000000FF00000000U)
        | ((a[4] << 24) & 0x00000000FF000000U)
        | ((a[5] << 16) & 0x0000000000FF0000U)
        | ((a[6] <<  8) & 0x000000000000FF00U)
        | (a[7]        & 0x00000000000000FFU);
      return n;
    }
    

    In this way you'll cast the char to a 64bit number before doing the shift and you won't go over range. You'll obtain correct results:

    entity:Dev jack$ ./a.out 
    aNum = 123456789
    bNum = 51544720029426255
    

    Just a side note, I think this would work fine too, assuming you don't need to peek inside the char array:

    #include 
    
    void int64ToChar(char a[], int64_t n) {
      memcpy(a, &n, 8);
    }
    
    int64_t charTo64bitNum(char a[]) {
      int64_t n = 0;
      memcpy(&n, a, 8);
      return n;
    }
    

提交回复
热议问题