Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

char  convert

Posted on 2003-11-24
Medium Priority
872 Views
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?
0
Question by:iux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 9

Expert Comment

ID: 9810058
Try using

#include <stdlib.h>

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

and in temp will be equal to 6

Hope this helps

Tincho
0

Author Comment

ID: 9810076
hmmmmmmmmmmm
it  says
invalid conversion from `char' to `const char*'
0

LVL 39

Accepted Solution

itsmeandnobodyelse earned 1200 total points
ID: 9810087
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

0

LVL 17

Expert Comment

ID: 9810103
char number = '6';
int temp =  number-'0';
0

LVL 3

Expert Comment

ID: 9811279
#define MAX ??
char arr[MAX];
char number='6';
arr[0]=number;
arr[1]='\0';

int val = atoi(arr);

RJ
0

LVL 4

Expert Comment

ID: 9812270
rstaveley's solution is simple and clean. Why go through all these complexity? :D
0

Author Comment

ID: 9812856
ok
i try it
thanks a lot
0

LVL 4

Expert Comment

ID: 9816957
... 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
0

Author Comment

ID: 9824981
thx :-)
0

Featured Post

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on thā¦
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locā¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month6 days, 1 hour left to enroll

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

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