Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 17-02-2009
Member
 
Join Date: Feb 2009
Posts: 10
How to convert a character to ASCII in C++?

Hi,

How to convert a character to ASCII in C++?.

Regards,
Reply With Quote
  #2  
Old 18-02-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: How to convert a character to ASCII in C++?

Please try this code!
Code:
#include <iostream.h>
#include <stdlib.h>

int main()
{
        int number;
        char letter;
        int option;



        cout<<"Convert ASCII numbers into characters and vice versa";
        cout<<endl<<"[1] * ASCII -> ABC"
                <<endl<<"[2] * ABC -> ASCII"
                <<endl<<"[3] * EXIT"<<endl;;

        cin>>option;
        switch (option) //Detects the option
               {
                case 1:
                        cout<<"Enter a number : ";
                        cin >> number; //Inputs the number
                        cout<<"The number you entered is : \""<<char(number)<<"\" in ASCII"<<endl; //Ouputs the same number in char
                        break;

                case 2:
                        cout<<"Enter a letter : ";
                        cin >> letter; //Inputs the letter
                        cout<<"The character you entered is : \""<<int(letter)<<"\" in ASCII"<<endl; //Outputs the same letter in int
                        break;

                case 3:
                        return 0;

                default: //If user chooses anything else besides options given
                        cout<<"Invalid Option!";
                        system("PAUSE");
                        break;
                }

        system("PAUSE");
        return 0;
}
Reply With Quote
  #3  
Old 18-02-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How to convert a character to ASCII in C++?

Here's what I've tried (I see now why this did not work...):

Code:
string myInput = Convert.ToString(Convert.ToChar(710));
byte[] asBytes = Encoding.ASCII.GetBytes(myInput);
But this does not result in 94 but a byte with value 63...
Here's a new try but it still does not work:

Code:
byte[] bytes = Encoding.ASCII.GetBytes("?");
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to convert a character to ASCII in C++?"
Thread Thread Starter Forum Replies Last Post
How to convert character data to integer data FFCookie Software Development 8 26-09-2010 03:28 AM
Convert ebcdic file to ASCII L-cynthiya Software Development 3 30-11-2009 05:09 PM
How to convert Image To HTML Files With ASCII Characters Francesca Software Development 2 26-03-2009 11:26 PM
Insert a special character in a document (ASCII codes) wingeek Customize Desktop 3 06-02-2009 10:46 PM
Need to convert string to ascii values with C# Shashikant Software Development 2 02-02-2009 07:14 PM


All times are GMT +5.5. The time now is 01:43 PM.