Solved

char  convert

Posted on 2003-11-24
9
862 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
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 300 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

by:rstaveley
ID: 9810103
char number = '6';
int temp =  number-'0';
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now