c++ - INTtoCHAR function couts wrong value -


here function looks like:

signed char inttochar(int int) {     signed char char = (signed char)int;     return char; }  int chartoint(signed char char) {     int int = (int)char;     return int; } 

it works assigns int value char, when want cout char gives me weired signs. compiles without errors.

my testing code is:

int main() {   int x = 5;   signed char after;   char compare = '5';   after = inttochar(5);    if(after == 5)   {       std::cout << "after:" << after << "/ compare: " << compare << std::endl;   }     return 0; } 

after indeed 5 doesn't print 5. ideas?

adding above answer using unary operator +, there way well: typecasting.

std::cout << "after:" << (int)after << "/ compare: " << compare << std::endl; 

correct output


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -