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

时间:2012-03-14 03:58:19

标签: c linux

我无法将int64_t转换为char数组并返回。我不知道下面的代码有什么问题,这对我来说完全合乎逻辑。该代码适用于a,如图所示,但不适用于明显属于int64_t范围的第二个数字b

#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

4 个答案:

答案 0 :(得分:7)

这很简单,问题是你在char数组中移位位,但a[i]的大小是4个byes(向上转换为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;
}

答案 1 :(得分:1)

void int64ToChar(char mesg[], int64_t num) {
    *(int64_t *)mesg = num; //or *(int64_t *)mesg = htonl(num);

}

答案 2 :(得分:0)

你是32位机器吗? IIRC我认为U后缀只表示'unsigned int'或'unsigned long',两者都是32位机器上的32位。我认为你需要一个'无符号长长'(64位)字面值,或0xFF00000000000000ULL

http://en.kioskea.net/faq/978-c-language-handling-64-bit-integers#unsigned-64-bit-integer

答案 3 :(得分:0)

charTo64bitNum中,您需要在转换之前将char转换为64位:

(((int64_t)a[0] << 56) & 0xFF00000000000000U)