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
9
Medium Priority
?
872 Views
Last Modified: 2010-04-01
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
Comment
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
9 Comments
 
LVL 9

Expert Comment

by:tinchos
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

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

Accepted Solution

by:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:rstaveley
ID: 9810103
char number = '6';
int temp =  number-'0';
0
 
LVL 3

Expert Comment

by:RJSoft
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

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

Author Comment

by:iux
ID: 9812856
ok
i try it
thanks a lot
0
 
LVL 4

Expert Comment

by:void_main
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

by:iux
ID: 9824981
thx :-)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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.

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.

Join & Ask a Question