C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 24th, 2005, 01:58 AM
doneirik doneirik is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 24 doneirik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 24th, 2005, 02:10 AM
wannabe wannabe is offline
=) wannabe?
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2002
Location: florida
Posts: 2,153 wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 13
Send a message via AIM to wannabe Send a message via Yahoo to wannabe
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.

Reply With Quote
  #3  
Old January 24th, 2005, 05:12 AM
Jaspreet Singh Jaspreet Singh is offline
Getting my *facts* right
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Amritsar, India
Posts: 177 Jaspreet Singh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old January 24th, 2005, 05:13 AM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,509 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 17 h 53 m 58 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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.

Reply With Quote
  #5  
Old January 24th, 2005, 09:26 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,345 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 38 m 38 sec
Reputation Power: 54
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > char to int

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap