char convert

i  wanna convert  char,which is read from a  data, convert  into the type  int.
i have tried  
int tmp; tmp = char;
but  it returns the ASCII number,
emaple: char  ch = '6';
(int)ch = 54

how can i do it?
and which head date should i add?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try using

#include <stdlib.h>

char number = '6';
int temp = atoi( number );

and in temp will be equal to 6

Hope this helps

iuxAuthor Commented:
it  says
invalid conversion from `char' to `const char*'
No, atoi wants a char* - char pointer - and not a single char.

if you have a single digit in a char you may do this:

  char c = '9';

  int    i  = c - '0';

maybe you should check before:

   if (c >= '0' && c <= '9')
        int i = c - '0';

if you have a number and not a digit, you may use atoi

   char cn[] = "123";   // that i a zero-terminated string
   int    i      = atoi(cn);

Regards, Alex

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

char number = '6';
int temp =  number-'0';
#define MAX ??
char arr[MAX];
char number='6';

int val = atoi(arr);

rstaveley's solution is simple and clean. Why go through all these complexity? :D
iuxAuthor Commented:
i try it
thanks a lot
... and what will you do if the number is something like this: 12345 ???

this will not work 'cause "12345" is not a single char!

therefore this will do it, and even with one char!


char *number = "12345";
int realNumber = atoi(number);

but don't do this:
char *number = '12089';
char *number = '2';

best regards
iuxAuthor Commented:
thx :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.