x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 297

# Converting a char to an integer

Hi!

How do you convert a character to an integer within a string?

eg. "dictX.txt"

How do you change the 'X' to an integer?
0
aine
• 4
• 3
• 2
• +2
1 Solution

Commented:
forget it is a char and treat it as an integer

here is some code -

char buff[]="dict4.txt";
int l;
sscanf(buff,"dict%d.txt",&l);
0

Commented:
Do you really mean to convert an "X" to an integer, or do you mean to convert a digit that is in the place you represented with the X to an integer.   Shaun has shown you how to do the 2nd case.
0

Author Commented:
This solution does not convert an integer to another integer.I want it to increment it..as in dict1.txt then dict2.txt etc!!

eg.

#include <stdio.h>

int Count=0;
int NumFiles=9;

void main()
{
while (Count<NumFiles)
{
char buff[]="dict4.txt";
int l;
sscanf(buff,"dict%d.txt",&l);
l++;

}
Count++;
}
0

Author Commented:
This solution does not convert an integer to another integer. I want it to increment it..as in dict1.txt then dict2.txt etc!!

eg.

#include <stdio.h>

int Count=0;
int NumFiles=9;

void main()
{
while (Count<NumFiles)
{
char buff[]="dict4.txt";
int l;
sscanf(buff,"dict%d.txt",&l);
l++;

}
Count++;
}

0

Commented:
>> This solution does not convert an
>> integer to another integer.
Well that is not what you originally asked for.

If the numbers that appear in the string will only be single digits, then just do

int main()
{
char buff[]="dict4.txt";

while (Count<NumFiles)
{
dict[4]++; // increment the 5th character.
Count++;
}
}

Otherwise you will need to use something like sscanf to find the integer and sprintf to recreate the string with the new integer value, like

int main()
{
char buff[]="dict4.txt";

while (Count<NumFiles)
{
int l;
sscanf(buff,"dict%d.txt",&l);
++I;
sprintf(buff,"dict%i.txt",I);
Count++;
}
}
0

Commented:
0

Commented:
if your trying to get the ascii code then

printf("The ascii code is %i\n",char_array[4]);

single chars are stored as a number (int) now a char is only i byte so your putting a smaller data type into a larger one

so

int i = char_array[4];

should be fine
0

Commented:
That won't even compile.  And if it did it would not be safe or portable.  If you want to print the ASCII value of character, just treat it as a number like

char Ch = 'A';
printf("The ascii code is %i\n",(int) Ch);

(Technically the (int) isn't even needed, it should be done automatically, but I think it makes it clearer.)
0

Commented:
nietod: printf isn't typesave!

So:

char c = 'A';
printf("%i", c);

won't work, because printf reads an int (%i) while only a char is provided.
If it doen't crash the system, it will at least give a wrong result.
So the (int) is necessairy...
0

Author Commented:
Sorry for being vague in my question.....but the second solution was very clear and concise...thanks.
0

Commented:
>> nietod: printf isn't typesave!
Agreed.  I don't use it or scanf() etc anywhere in my code.  But I was trying to "work with" shaun and markerp's code.

aine, I purposefully did not answer the question because you had rejected Shaun's answer a little unfairly--at least in my opinion.  If an expert's answer is not adequite--especially if it is because you didn't ask the entire question--then you should give the expert a chance to finish the question, rather than rejecting their answer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.