Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting element in char array to int

Tags:

c++

arrays

char

I have an 80 element char array and I am trying to specific elements to an integer and am getting some number errors. Array element 40 in hex is 0xC0. When I try assigning it to an integer I get in hex 0xFFFFC0, and I dont know why.

char tempArray[80]; //Read in from file, with element 40 as 0xC0
int tempInt = (int)tempArray[40]; //Output as 0xFFFFC0 instead of 0x0000C0
like image 417
user2840470 Avatar asked Jul 21 '14 15:07

user2840470


1 Answers

Depending on your implementation, a char type in C++ is either a signed type or an unsigned type. (The C++ standard mandates that an implementation chooses either scheme).

To be on the safe side, use unsigned char in your case.

like image 183
Bathsheba Avatar answered Oct 14 '22 18:10

Bathsheba