473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to convert character to integer c++ (urgent )

hiii
i am taking input from a file as a character and have to convert it into integer to do some operations
eg the character input may be 1 , 0 , 2 etc.
and i need to convert them into integers ..
i have tried doing
unsigned char var_name;
int i;
i=int( var_name);

but it gives wrong output .. probably it is converting the character into ascii..
i cannot understand how to go about it ..
please help ...
Jun 19 '07 #1
4 35963
Meetee
931 Expert Mod 512MB
hiii
i am taking input from a file as a character and have to convert it into integer to do some operations
eg the character input may be 1 , 0 , 2 etc.
and i need to convert them into integers ..
i have tried doing
unsigned char var_name;
int i;
i=int( var_name);

but it gives wrong output .. probably it is converting the character into ascii..
i cannot understand how to go about it ..
please help ...

Use atoi function to convert char to int

Cheers!
Jun 19 '07 #2
Use atoi function to convert char to int

Cheers!
hii
thankyou!!
it works ..
by the way .. why cant it convert unsigned char* to const char * ... imean it works when i declare the variable character .. but does not in case i declare the variable unsigned char .. :)
Jun 19 '07 #3
Meetee
931 Expert Mod 512MB
hii
thankyou!!
it works ..
by the way .. why cant it convert unsigned char* to const char * ... imean it works when i declare the variable character .. but does not in case i declare the variable unsigned char .. :)
For that try this:

unsigned char tmp = 'a';
int num = static_cast<int>(tmp);

For the sake of knowledge:
The static_cast operator converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion.( from http://msdn2.microsoft.com/en-us/library/ms861534.aspx)

Regards
Jun 19 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
You don't need atoi. That's a relic C function.

You convert a char to an int by assigning the char to the int.

Expand|Select|Wrap|Line Numbers
  1. char c = 'A';
  2. int x;
  3. x = c;    //x contains 65 which is the ASCII value of 'A'
  4. c = '2';
  5. x = c;    //x contains 62 which is the ASCII value of '2'
  6. x = c - 48;  //x contains 2  
  7.  
A char is just an 8-but integer so you can freely use as an integer.
Jun 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
14
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
2
by: guest124 | last post by:
Hi, I am having problem in converting an integer to character in the following program. It compiles but at run time there is no value in c. Could anyone help me with the problem. It is very...
4
by: sagar | last post by:
Hi Guys.. I Wanted to convert an int to a string in C#...but i cannot use any built in functions.So, can any one tell me an algorithm or a program to do so....please is urgent....
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.