简体   繁体   中英

64bit hex to decimal in C

I am reading in a pcap file from Wireshark and storing the data in 8bits using u_char. One portion of the data is in 64bits, so I have eight 8bit numbers that I need to combine into/ read as one 64bit number and then convert it into decimal. For example, this is what I currently have

Hex: 3f e2  da  2f 8b  de  c5  f4
Dec: 63 226 218 47 139 222 197 244

and this is what I currently want,

Hex: 3fe2da2f8bdec5f4
Dec: .589134

To combine the 8 1bytes, I have tried:

long long int a;
a = (data[j] << 56) | (data[j+1] << 48) | (data[j+2] << 40) | (data[j+3] << 32) | (data[j+4] << 24) | (data[j+5] << 16) | (data[j+6] << 8) | data[j+7];
printf("%lld", a);

output: -1073815553

Help is greatly appreciated. Thanks.

Looks like OP wants to overlay an 8-byte integer with a binary64 double .

Such code is portable to many machines, but is certainly not portable to all C machines as is assumes many things like the form of a double .

int main(void) {
  union {
    double d;
    unsigned long long ull;
  } u;
  u.d = 0.589134;
  printf("%016llX\n", u.ull);
  u.ull = 0x3fe2da2f8bdec5f4;
  printf("%lf\n", u.d);
  return 0;
}

3FE2DA2F8BDEC5F4
0.589134

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM