Results 1 to 3 of 3

Thread: How to convert a character to ASCII in C++?

  1. #1
    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,

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

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

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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("ê");

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,967,911.60225 seconds with 16 queries