Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

convert character to Ascii in C++???

ascii

  • Please log in to reply
8 replies to this topic

#1 fire exit

fire exit

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 24 September 2007 - 07:43 PM

i am C++ noobies. how can i change a character into ascii then move it into variable?:confused:
  • 0

#2 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 24 September 2007 - 08:49 PM

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.
// 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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 fire exit

fire exit

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 24 September 2007 - 10:16 PM

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.
  • 0

#4 ZedMan

ZedMan

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 September 2007 - 11:01 PM

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.


int asciiVal = 65;

//then either

char asciiChar = static_cast<char>(asciiVal);

//or simply

char asciiChar = asciiVal;

  • 0

#5 MaxGuru

MaxGuru

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 29 October 2007 - 04:26 AM

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)


[COLOR="Blue"] int [/COLOR] asciiVal = 0;

[COLOR="Blue"] char [/COLOR] letter = 'A'; 

[COLOR="Green"]//this will  cast to the ascii value (int) [/COLOR]
asciiVal = ([COLOR="Blue"]int[/COLOR])letter;


This should work nice for you
  • 0

#6 chest069

chest069

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 11 May 2008 - 07:13 AM

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;
}
  • 0

#7 shalini

shalini

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 23 September 2010 - 12:57 AM

in C++,how can i restrict user to enter only character type value in character type variable and integer type value in integer type variable at RUN TIME. i want to do it by checking ascii values in both case. plz. help.
  • 0

#8 shalini

shalini

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 23 September 2010 - 01:04 AM

How to restrict user to enter values according to datatype at runtime

Hi, In C++,how can i restrict user to enter only character type value in character type variable and integer type value in integer type variable at RUN TIME. i want to do it by checking ascii values in both case. plz. help.
  • 0

#9 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 23 September 2010 - 02:35 PM

Generally, you have to accept the input as a string, and validate it/convert it to the appropriate data type after accepting it.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download