# Thread: Conversion of character string to integer

1. ## Conversion of character string to integer

How do I convert a character to an integer? For example if I have a character '1' how do I convert it into an integer type one. There is a good news and bad news. The bad news is that my character array starts out with "non-integer" characters and "integer" characters that gets tokenized and pushed into a queue data structure. The good news is that I'll be able to check if each element is an integer is not by some sort of test.
Code:
```queue <char> Q1;
// after loading characters string, we need to see if each element is an integer

t = Q1.front();
if (t == integer)
// do mathematical operation
else {
// do something else
}```
So, my question is how would you convert that character into a mathematical element? Thank you.

2. I don't know where you're going with that if statement, but it looks rather doomed to me.

As for converting characters to numberals, there are two easy ways to do this. One is atoi(). The other makes use of characters' ASCII values:
Code:
```char ch;
int x;
cout << "Enter a digit: ";
cin >> ch;
ch -= '0';
x = ch;
cout << x;```
There, try that. If I screwed up a bit, look up an ASCII table to get the ASCII value exactly right. I think the numbers go 0-9. This solution will only work for a single digit, though. For larger numbers, you have to parse through a string character by characer.

3. If you're just using single characters (not strings), then it is rather simple:

if('0' <= x && x <= '9') // Then x is an integer.

Note, joshdick's solution works fine because '0' will convert to the proper integer value for you (60 something, I think), without having to know it or rely on it.

4. I tend to use isalpha(string);

not of isalpha will tell wheather a string forms a number...... or you can use atoi which will throw an exception in case it is not a string..