How to cast hex value to WORD, DWORD or QWORD and store the result in a double variable?

Viewed 3140

I'm trying to cast a signed hex number to WORD, DWORD and QWORD by this way:

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

int main(void) {

  printf("WORD=%d\n",  (int16_t) strtol("F123", NULL, 16));
  printf("DWORD=%d\n", (int32_t) strtol("FFFFF123", NULL, 16));
  printf("QWORD=%lld\n", (int64_t) strtol("FFFFFFFFFFFFF123", NULL, 16));

  return 0;
}

But it returns the following:

WORD=-3805
DWORD=2147483647
QWORD=2147483647

http://ideone.com/mqjldk

But why the DWORD and QWORD castings are not returning -3805 too?

I mean: 0xFFFFF123 stored in a DWORD would contain -3805 value in decimal, not 2147483647

Expected output:

WORD=-3805
DWORD=-3805
QWORD=-3805

Do you have an bitwise alternative to do it?

4 Answers
Related