hi, just wondering is there a function that converts a single char to an int. The parameter being passed needs to be a char as well, thanks.
hi, just wondering is there a function that converts a single char to an int. The parameter being passed needs to be a char as well, thanks.
Be a leader and not a follower.
atoi(). Pass the address of a char.
I've tried atoi but it will only allow strings as the parameter,
Be a leader and not a follower.
Just throwing a guess in the air, maybe you could typecast it: num=atoi((char*)yourchar). If all else fails, you could put the char into a string then use that.
It 'only' allows strings because it's expecting the start address of a string. Just pass the address of a char.
atoi() will only accept strings. This is by design.
Use that.Code:int ctoi( int c ) { return c - '0'; }
Quzah.
Hope is the first step on the road to disappointment.
Soz for the late reply but i've been working at late, would you be able to explain how the following code works, thanks.
Code:return c - '0'
Be a leader and not a follower.
Figured it out, the ascii val to '0' is 48, therefore the code is basically subtracting 48 from the argument passed due to the int return type. Ascii code for '3' is 51, so 51 - 48 is (3).
Be a leader and not a follower.
why not try casting?
Code:... unsigned char x= '?'; cout<<int(x); ...