Solved

char  convert

Posted on 2003-11-24
9
866 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.
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.

828 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