# char convert

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

how can i do it?
###### 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.

Commented:
Try using

#include <stdlib.h>

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

and in temp will be equal to 6

Hope this helps

Tincho
Author Commented:
hmmmmmmmmmmm
it  says
invalid conversion from `char' to `const char*'
Commented:
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

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

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

int val = atoi(arr);

RJ
Commented:
rstaveley's solution is simple and clean. Why go through all these complexity? :D
Author Commented:
ok
i try it
thanks a lot
Commented:
... 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!

look:

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

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

best regards
void_main
Author 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
C++

From novice to tech pro — start learning today.