character in ascii code

to change a character to integer in ascii code

Page 1 of 1

6 Replies - 2813 Views - Last Post: 30 May 2009 - 12:53 PM Rate Topic: -----

#1 sayalew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-May 09

character in ascii code

Posted 30 May 2009 - 02:00 AM

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int i;
cout<<"enter your character form the key board";
cin>>ch;
i=int.ch;
cout<<"your character "<<ch<<" in ascii code is "<<i;
getch();
}
.cpp


*Mod edit: fixed code tags: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: character in ascii code

#2 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: character in ascii code

Posted 30 May 2009 - 02:48 AM

Did you have a question?

Does int.ch work?
Was This Post Helpful? 0
  • +
  • -

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: character in ascii code

Posted 30 May 2009 - 02:50 AM

:D Hello,

Give this a try:



#include <iostream>
#include <windows.h>

/*---------------------
|	 NameSpace	 |
---------------------*/

using namespace std;

/*----------------------------
|	  Define Constants	  |
----------------------------*/

/*--------------------------
| Function Declarations   |
--------------------------*/



/*-------------
|	main	 |
-------------*/
int main(int argc, char *argv[])
{
	int  choice = 0;
	bool exit   = true;
	
	do
	{
		
		system("CLS");

		char ch = NULL;
		
		cout << endl << endl << endl;
		cout << "		  Enter your character form the key board" << endl << endl;
		cout << "		  and press return => ";
		cin >> ch;
		cout << endl << endl << endl;

		
		cout << "		  Your character is " << ch << endl << endl;
		cout << "		  In ascii code it is " << int(ch);

				
		cout << endl << endl << endl;
		cout << "		  ";
		system("PAUSE");

		cout << endl << endl << endl;
		cout << "		  Type the number 1 and press return to run this program again;" << endl << endl << endl;
		cout << "		  or type any key and press return to exit ==> ";
				
		cin >> choice;
					
		if (choice == 1)
				exit = true;
		else
			exit = false;
	}while(exit == true);

  
	return 0;
}



/*-------------
|  Functions  |
-------------*/



/*---------------------
|   End of Program	|
---------------------*/


Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: character in ascii code

Posted 30 May 2009 - 06:24 AM

@Elcric -- you should probably try not to use exit as a variable name. Its not a reserved word or anything. The police will not show up at your house, but it is a standard function in the cstdlib and you might mask the function or cause a moment of confusion when someone is looking over your code... :)


plus it would be more logical if you loop was "while(exit == false)" -- at least to me. if exit is true I want to leave, if exit is false I don't want to leave...

I like it when programming is a nice logical language that speaks to me.
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: character in ascii code

Posted 30 May 2009 - 09:16 AM

Internally, there's no such thing as a character. Characters are stored as ASCII Numbers in memory.

It's precisely why you can treat characters as integers in a loop construct like this:
for(int i='A'; i < 'Z'; i++)
   printf("\n%c",i);



So all you need to do is typecast a character to an int to get its ASCII code or viceversa if you want the character equivalent of a number.

Character to ASCII:
char x = 'A';
cout<<(int)x;



If you're using printf(), you dont even need to typecast it. You just need to specify the output format to printf. So we simple ask printf() to output a character or number depending on what you want to do like this:

char c ='A';
int i = 70;

//Char to ASCII
printf("%c = %d",c,c);

//ASCII to char
printf("%d = %c",i,i);


Hope this helps.
:)
Was This Post Helpful? 0
  • +
  • -

#6 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: character in ascii code

Posted 30 May 2009 - 11:44 AM

Ahhh, the freedoms of coding. I do like printf() as a function when trying o get output into certain formats, whether it be hex, ascii or integer, it's always nice to just be able to throw a 0x%08x or a %c or %i Always enjoyable. I like Born2c0de's way

This post has been edited by IngeniousHax: 30 May 2009 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: character in ascii code

Posted 30 May 2009 - 12:53 PM

what a confusion ...

int StrToInt(const char* &Str)
{
	int sum = 0, factor = 1;
	if(Str!=NULL)
	{
		int size = sizeof(Str);
		while(size-->0)
		{
			if(Str[size]<48 || Str[size]>57)
				break;

			sum += ((Str[size] - 48) * factor);
			factor *= 10;
		}
		return sum;
	}
	return 0;
}



and if you want to check the ASCII code for any character ...


	char Str[] = "plus";

	cout << "\n\n  " << Str << '\t';

	cout << "[ ";
	for(int i=0;Str[i]!='\0';i++)
		cout << (int)Str[i] << " | ";
	cout << " ]\n\n  ";



This post has been edited by Plus: 30 May 2009 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1