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?
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?
ASKER
hmmmmmmmmmmm
it says
invalid conversion from `char' to `const char*'
it says
invalid conversion from `char' to `const char*'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
char number = '6';
int temp = number-'0';
int temp = number-'0';
#define MAX ??
char arr[MAX];
char number='6';
arr[0]=number;
arr[1]='\0';
int val = atoi(arr);
RJ
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
ASKER
ok
i try it
thanks a lot
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
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
ASKER
thx :-)
#include <stdlib.h>
char number = '6';
int temp = atoi( number );
and in temp will be equal to 6
Hope this helps
Tincho