Results 1 to 3 of 3

Thread: Converting Char value to ASCII number, C++

  1. #1
    Join Date
    Apr 2009
    Posts
    3

    Converting Char value to ASCII number, C++

    Hey guys,

    i know there is already a post like this up, and i'v read it but it's not really helping me.
    I need to convert a char value into it's ASCII number equivilent.
    The letter is an input from the user so it's completly random, and it's just a single letter.
    I know how to write the program so that it just outputs the value as a number but what i actually need to do is convert it and then assign that number to a different variable.
    I'm assuming there is a particular function i can use, i just don't know what it is!
    If worst comes to worst i suposse i could just write an if statment for every letter of the alphabet but i'm hoping there is an easier way XD!

  2. #2
    Join Date
    Mar 2008
    Posts
    198

    Re: Coverting Char value to ASCII number, C++

    Refer to this thread: How to convert a character to ASCII in C++?

    Hope this will help. Good luck

  3. #3
    Join Date
    Oct 2008
    Posts
    167

    Re: Coverting Char value to ASCII number, C++

    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.

    Code:
    // 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.

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2011, 10:38 AM
  2. Converting a string number into sequence of digits
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 13-03-2010, 04:46 PM
  3. Converting Char to Int in PHP
    By GlassFish in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 08:06 PM
  4. Converting string to char
    By Abdullah30 in forum Software Development
    Replies: 3
    Last Post: 03-09-2009, 11:09 PM
  5. How to Create ASCII Art
    By avvia in forum Windows Software
    Replies: 3
    Last Post: 29-06-2009, 11:06 AM

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,714,225,392.46514 seconds with 17 queries