一尘不染

如何将64位整数转换为char数组并返回?

linux

我在将int64_t转换为char数组并返回时遇到麻烦。我不知道下面的代码有什么问题,对我来说,这完全符合逻辑。该代码适用于a如图所示的代码,但第二个数字b显然不在int64_t范围内。

#include <stdio.h>
#include <stdint.h>

void int64ToChar(char mesg[], int64_t num) {
  for(int i = 0; i < 8; i++) mesg[i] = num >> (8-1-i)*8;
}

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

int main(int argc, char *argv[]) {
  int64_t a = 123456789;
  char *aStr = new char[8];
  int64ToChar(aStr, a);
  int64_t aNum = charTo64bitNum(aStr);
  printf("aNum = %lld\n",aNum);

  int64_t b = 51544720029426255;
  char *bStr = new char[8];
  int64ToChar(bStr, b);
  int64_t bNum = charTo64bitNum(bStr);
  printf("bNum = %lld\n",bNum);
  return 0;
}

输出是

aNum = 123456789
bNum = 71777215744221775

该代码还给出了两个我不知道如何摆脱的警告。

warning: integer constant is too large for ‘unsigned long’ type
warning: left shift count >= width of type

阅读 301

收藏
2020-06-07

共1个答案

一尘不染

这很简单,问题在于您正在移位char数组中的位,但是大小a[i]为4个字节(向上转换为int),因此您的移位超出了范围。尝试将其替换为您的代码:

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;
}

这样,您将char在转换之前将强制转换为64位数字,并且不会超出范围。您将获得正确的结果:

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

附带说明一下,假设您不需要窥视char数组,我认为这也可以正常工作:

#include <string.h>

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;
}
2020-06-07