I am getting strings from an HTTP request that will have hex values and I must convert those strings to a signed decimal.
1 2 3 4 5 6 7 8
//typical string inside response: //0E1D052BFBB711C1002C0042007A014DFE44022B270F7FFF8000000000000000
//every 4 characters above are a signed decimal value
for (a = 0; a <= 63; a+=4){
sprintf(vval,"0X%c%c%c%c",response[a],response[a+1],response[a+2],response[a+3]);
ds = strtol(vval, NULL, 16);
sprintf(vval,"%d",ds);
}
The problem is I never see a negative number. Decoding 0x8000 gives me 32768 but not -32768.
0x8000 short is negative but 0x00008000 long is positive number.
You need to have 0xFFFF8000 to see it as negative. So as doug4 wrote: make vval short or sign extend short to 4 bytes: vval = (vval<<16)>>16;
before using it in strtol