English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

C++ program to convert number to ascii number?
i have been given an assignment to take a number input from user and convert it into its equivalent hexadecimal, octal and ascii values. i have done the hex and oct but i am sttruck on ascii can anyone plz help me....
(I NEED THE PROGRAM -> to convert user input number to ascii)
for example if the user inputs 65 the output should be A

2007-02-03 05:57:33 · 6 answers · asked by jijo p 2 in Computers & Internet Programming & Design

6 answers

friend i have done it in C. as printf() and scanf() is allowed in C++ too it will work for you.

#include
#include

void main()
{
int num;
clrscr();

printf("Enter the number: ");
scanf("%d",&num);

printf("The ascii equivalent is: %c",num);
getch();
}

2007-02-03 06:07:36 · answer #1 · answered by Anonymous · 0 0

Use the C-Standard function sprintf for those purposes: int number = 1243; char str[1024]; sprintf(str, "%i", number); That should do it.

2016-03-29 03:11:31 · answer #2 · answered by Anonymous · 0 0

//tested in MS VC++ 2005
#include "stdafx.h"
#include

int _tmain(int argc, _TCHAR* argv[])
{

int nr;//integer
char c;

std::cout << "Give me a positive number less than 255:\t" ;
std::cin >> nr;
if (nr >=1 && nr <= 254 )
{
c = (char)nr;//character from ASCII code using type-casting
std::cout << "ASCII from " << nr << " is " << c << "\n";
}
else
{
std::cout << "\n\a Error!! Need a positive number less than 255 \n" ;
std::cout << "\t\t Have a nice day !!\n";
}
return 0;
}

2007-02-03 14:15:39 · answer #3 · answered by dand370 3 · 0 0

You do this by type-casting the int to a char.
e.g.. char a = (char)65;

the variable a now equals 'A'. and vice-versa. int a = (int)'A';
the -a- will now be equal to 65.

2007-02-03 07:09:10 · answer #4 · answered by Anonymous · 0 0

If you've got the others, then just add this to them...

char myChar;
...

if (input < 256) {
myChar = input;
cout << "Character value is: " << myChar << endl;
}

Of course, you could add better error checking to it

2007-02-03 06:03:41 · answer #5 · answered by BigRez 6 · 0 0

here is the program
main()
{
int a=65;
printf("%d \n\t %c",&a,&a);
}

2007-02-03 06:09:33 · answer #6 · answered by abishek j 2 · 0 0

fedest.com, questions and answers