Help convert char to int

This is a discussion on Help convert char to int within the C++ Programming forums, part of the General Programming Boards category; I know how to convert a char to an int using atoi but how do I convert a single digit ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    16

    Help convert char to int

    I know how to convert a char to an int using atoi but how do I convert a single digit int to a char???? thanks.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try itoa() if your compiler comes with the non-standard conio.h file and your are willing to use non-standard material or use the standard compliant stringstream class and extract the char you want from the string which is derived.

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    use itoa. It's not portable so, if you just want a sigle int to char, you could do something like:
    Code:
    char int_to_char(int input)
    {
      if(input<0 || input>9){
        std::cerr<<"Wrong int taken as input"<<std::endl;
        return '0';
      }else{
      return reinterpret_cast<char>(input + 48);
      }
    }
    What we do is:
    First, we check that the number is appropiate for conversion.
    If the number is not right, then we output error and return a zero (char), because we need to return something.
    We add 48 to the int. This gives us the ascii code for the number (48 ascii is 0 int).
    Then we let the compiler reinterpret the number as a char, that way, the correct number will be returned.

    This will only work for numbers between 0 and 9. No other numbers will be made into chars.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I know how to convert a char to an int using atoi but how do I convert a single digit int to a char????
    Provided the int is really a single digit you can simply add '0' to it:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int num = 5;
      char digit = char ( num + '0' );
    
      cout<< num <<'\t'<< digit <<endl;
    }
    If at any point you want multiple digit numbers to a string you will need to use sprintf or stringstreams. Don't use itoa because there are standard alternatives.

    >return '0';
    Not a good error code. Perhaps returning an obviously invalid char, or throwing an exception would be better suited.

    >return reinterpret_cast<char>(input + 48);
    reinterpret_cast is used for type conversions between two types that are unrelated. There's no need for it here. A static_cast would be better for removing the warning. And why use 48? That just makes the function less portable. Use '0' instead to avoid assuming ASCII.
    My best code is written with the delete key.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I was going to point out what Prelude just did... instead of using char(input+48), which requires you to memorize that the character '0' is dec 48 in the ASCII chart, you could just do char(input+'0')

    that also is more readable... unless you put a comment saying that 48 is the ASCII dec equivalent for '0', people might not know what you're talking about
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21