|
| |||||||||||||||||||||||||||||||||||||||||||||||||
| scienceskillz (8) | |
|
Hi, I have some code that looks like: char *response = new char[7]; unsigned short i; double cTemp; the output of : for(i=0;i<=6;i++) { cout << hex << (int) response[i] << endl; } is: 1 3 2 0 fffffff7 fffffff9 ffffffc2 Now the confusion comes in because I do: cTemp = ((0x00<<8) + (0xf7)); cout << cTemp << endl; which outputs: 247 as expected... However, When I do: cTemp = ((response[3]<<8) + response[4]); cout << cTemp << endl; which outputs : -9... Why are the two outputs different? | |
|
|
|
| Bazzy (6258) | |
| Did you assign a value to the array elements? | |
|
Last edited on
|
|
| scienceskillz (8) | |
|
Yes the values to the array elements come from the return from a com Port which looks like: int Tserial::get Array (char*buffer, int len) { unsigned long read_nbr; read_nbr = 0; if(serial_handle!= INVALID_HANDLE_VALUE) { ReadFile(serial_handle, buffer, len, &read_nbr,NULL); } return ( (int) read_nbr ); } and is called from a function like: Tserial com; com.connect("COM3",9600,spNONE); com.sendArray(command,8); Sleep(30); com.getArray(response,7); com.disconnect(); | |
|
|
|
| scienceskillz (8) | |
|
Also, The array elements have to be defined I would think otherwize: for(i=0;i<=6;i++) { cout << hex << (int) response[i] << endl; } would not output anything? | |
|
|
|
| scienceskillz (8) | |
|
Well, I guess I didn't really get this answered from this forum but after playing around a bit if anyone ever runs into a similar issue this worked for me: cTemp = ((response[3]&0xff) + response[4]&0xff)) cout << cTemp << endl; which outputs 247 | |
|
|
|
| firedraco (4744) | |
char is signed usually, so if you go over 127 (?) then you will start getting negative numbers.
| |
|
|
|