Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-24-2007, 10:43 PM
fire exit fire exit is offline
Newbie
 
Join Date: Jul 2007
Posts: 9
Rep Power: 0
fire exit is on a distinguished road
Default convert character to Ascii in C++???

i am C++ noobies. how can i change a character into ascii then move it into variable?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-24-2007, 11:49 PM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,880
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default

It's simple. A character that you're using in your program already have an ASCII-value, but you sees it like a character. If you want to have the value itself, you can simply convert the character to an integer.
Code:
// C, with C type-casting
char cMyCharacter = 'A';
int iMyAsciiValue = (int)cMyCharacter;

// C++, with C++ type-casting
char cMyCharacter = 'A';
int iMyAsciiValue = static_cast<int>(cMyCharacter);
You can actually leave out the type-casting part. C/C++ is built in the way that when an integer-variable meets a char-variable, it automatic converts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-25-2007, 01:16 AM
fire exit fire exit is offline
Newbie
 
Join Date: Jul 2007
Posts: 9
Rep Power: 0
fire exit is on a distinguished road
Default

What if i want to convert ASCII to Character.how can i do that?

Thanks sir. You are a big help to us programmer( both newbies and Pros). Thanks again.

Last edited by fire exit; 09-26-2007 at 01:24 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-27-2007, 02:01 AM
ZedMan ZedMan is offline
Newbie
 
Join Date: Sep 2007
Posts: 1
Rep Power: 0
ZedMan is on a distinguished road
Default

The same thing, just in reverse. You can either explicitly cast an int as a char or simply assign an int variable to a char variable.

C++ Code:
  1. int asciiVal = 65;
  2.  
  3. //then either
  4.  
  5. char asciiChar = static_cast<char>(asciiVal);
  6.  
  7. //or simply
  8.  
  9. char asciiChar = asciiVal;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-29-2007, 07:26 AM
MaxGuru's Avatar   
MaxGuru MaxGuru is offline
Newbie
 
Join Date: Oct 2007
Location: Bingo
Posts: 2
Rep Power: 0
MaxGuru is on a distinguished road
Send a message via AIM to MaxGuru
Thumbs up Just (Cast) It!!

i was thinking on about what you need to do .. all you need to do is cast it to an int to get the ASCII value.

ex)

Code:
 int  asciiVal = 0;

 char  letter = 'A'; 

//this will  cast to the ascii value (int) 
asciiVal = (int)letter;
This should work nice for you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-11-2008, 10:13 AM
chest069 chest069 is offline
Newbie
 
Join Date: May 2008
Posts: 1
Rep Power: 0
chest069 is on a distinguished road
Default Re: convert character to Ascii in C++???

Hey Guys,

I am new to learning C++ and I am trying to make a C++ program to display an Ascii image. Can someone help me get started on how to make this work? Thanks. I have included the image I am trying to make and the code that I have created. Can you tell me what I am doing wrong and how to get started correctly if I am doing this wrong? It is supposed to be the Ubuntu image but in ascii.

Image

.-.
.-'``(|||)
,`\ \ `-`. 88 88
/ \ '``-. ` 88 88
.-. , `___: 88 88 88,888, 88 88 ,88888, 88888 88 88
(:: : ___ 88 88 88 88 88 88 88 88 88 88 88
`-` ` , : 88 88 88 88 88 88 88 88 88 88 88
\ / ,..-` , 88 88 88 88 88 88 88 88 88 88 88
`./ / .-.` '88888' '88888' '88888' 88 88 '8888 '88888'
LGB `-..-( )
`-`

Code

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout << " .-. ";
cout << " .-'``(|||) ";
cout << " ,`\ \ `-`. 88 88 ";
cout << " / \ '``-. ` 88 88 ";
cout << " .-. , `___: 88 88 88,888, 88 88 ,88888, 88888 88 88 ";
cout << " (:: : ___ 88 88 88 88 88 88 88 88 88 88 88 ";
cout << " `-` ` , : 88 88 88 88 88 88 88 88 88 88 88 ";
cout << " \ / ,..-` , 88 88 88 88 88 88 88 88 88 88 88 ";
cout << " `./ / .-.` '88888' '88888' '88888' 88 88 '8888 '88888' ";
cout << " LGB `-..-( ) ";
cout << " `-` ";

system("PAUSE");
return EXIT_SUCCESS;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert your photoshop document to a CSS in less than 30 seconds clookid Photoshop Tutorials 17 06-04-2008 11:36 AM
Convert matlab code to C/C++ on Linux Kank C and C++ 2 10-11-2007 02:09 AM
Convert XLS in PDF master.jimmy Visual Basic Programming 1 09-10-2007 03:00 AM
Please Help With A C Program!! siren C and C++ 7 04-17-2007 08:45 AM


All times are GMT -5. The time now is 06:38 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads