The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
char to int
Discuss char to int in the C Programming forum on Dev Shed. char to int C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 24th, 2005, 01:58 AM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 24
Time spent in forums: 5 h 50 m 17 sec
Reputation Power: 0
|
|
|
char to int
..good morning
could anybody tell me how to convert a char to an int in C++ (and which file I need to include)
doneirik
|

January 24th, 2005, 02:10 AM
|
|
=) wannabe?
|
|
Join Date: Jul 2002
Location: florida
Posts: 2,153
 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 13
|
|
you dont need to include anything extra
Code:
char charVar = 'A';
int intVar = 0;
intVar = charVar; // charVar will be converted to int
intVar = static_cast<int>(charVar); // charVar will converted to int and you wanted it to =D
with the static_cast<>() thingy it's pretty much that you force a conversion you meant to happen. maybe char to int isnt the best example. but say if u convert float to an int purposefully it's useful. it's also used for other things i dont know about yet =)
Last edited by wannabe : January 24th, 2005 at 02:22 AM.
|

January 24th, 2005, 05:12 AM
|
|
Getting my *facts* right
|
|
Join Date: Mar 2004
Location: Amritsar, India
Posts: 177
Time spent in forums: 1 Day 17 h 40 m
Reputation Power: 10
|
|
and if the char contains a digit and you want that, then subtract 48 from the char.
Eg:
Code:
char ch='8';
int a;
a = ch-48;//a now contains 8.
also you can check the char to see if it's a digit or not using isdigit() function.
__________________
IMHO
|

January 24th, 2005, 05:13 AM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
The compiler will automatically make certain promotions when you assign one type of variable to another. Good investment to read your language/compiler documentation on coffee breaks or something.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
|

January 24th, 2005, 09:26 AM
|
|
|
|
Promotion from char to int occurs as specified as specified in the ANSI standards- for example passing a char as an argument to a function, or anytime you reference a char and an int in the same statement. In these cases a "cast" or promotion occurs whether you are aware of it or not.
Sometimes, it's good programming practice to cast even if you know it is gonna happen anyway. This lets the next guy see that:
1. you wanted it to happen
2. or you were aware of it.
These hidden promotions sometimes cause unintended side-effects.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|