Link to home
Start Free TrialLog in
Avatar of iux
iux

asked on

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?
and which head date should i add?
Avatar of tinchos
tinchos

Try using

#include <stdlib.h>


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

and in temp will be equal to 6

Hope this helps

Tincho
Avatar of iux

ASKER

hmmmmmmmmmmm
it  says
invalid conversion from `char' to `const char*'
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
char number = '6';
int temp =  number-'0';
#define MAX ??
char arr[MAX];
char number='6';
arr[0]=number;
arr[1]='\0';

int val = atoi(arr);

RJ
rstaveley's solution is simple and clean. Why go through all these complexity? :D
Avatar of iux

ASKER

ok
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!

look:


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

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

best regards
void_main
Avatar of iux

ASKER

thx :-)