Solved

Converting a char to an integer

Posted on 2000-04-10
11
282 Views
Last Modified: 2010-04-02
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
Comment
Question by:aine
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2701168
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
 
LVL 22

Expert Comment

by:nietod
ID: 2701741
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 Comment

by:aine
ID: 2701972
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 Comment

by:aine
ID: 2702016
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
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2702019
>> 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2703239
aine - sorry I answered your question as you asked it -
0
 
LVL 10

Expert Comment

by:makerp
ID: 2703879
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
 
LVL 22

Expert Comment

by:nietod
ID: 2703910
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
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2704331
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 Comment

by:aine
ID: 2704379
Sorry for being vague in my question.....but the second solution was very clear and concise...thanks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2704479
>> 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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now