dumbass newbie c++ question

Thom

Platinum Member
Oct 18, 1999
2,364
0
0
Okay, i know, this is probably real easy...but call me stupid.

how do i typecast a (char) into a (int) and get a decent value

for example if i have a char[2] "10" how to i typecast it into an integer=10

Thanks, i know i am probably stupid.

 

michaelh20

Senior member
Sep 4, 2000
482
0
0
ummm, I don't know c++, but I do know that "10" is a string and not an char :)

I think you have to get each char of the string starting from the right and multiply it by the right power of 10. (in the case 223, you have 3 x 10 ^ 0 + 2 x 10 ^ 1 + 2 x 10 ^ 2)

And if it just an ascii value like "1" or "2", you just have to subtract the right value from the ascii code (which you get when you do just a (int) cast.) So if you get 9999 for a value of 1, then subtract 9998 from it and you get the right values for 2, 3, 5,6 , etc... but I don't have the ascii chart memorized right off hand :)
 

Thom

Platinum Member
Oct 18, 1999
2,364
0
0
i've yanked the char out of a text file using ifstream. it will happily display the number if it is char[x]. so i don't think, (not that i have a clue) it's a string in that sense.
 

Thom

Platinum Member
Oct 18, 1999
2,364
0
0
er, okay, captain dumbass over here just changed the datatype to a int initially, and the problem went away. god i am stupid. cheers anyway mate. :D
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
uh, you can convert a string or character to an int using atoi.

syntax:

int x;
char blah[10];

.
.
.
.


x = atoi(blah);