Thread: atoi not converting chars to ints?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    60

    Question atoi not converting chars to ints?

    What should I use to convert character variables into integer variables? I need to do this for a thing I'm making. Any help will be appreciated.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    atoi not converting chars to ints?
    Care to expand on why this is not working for you?

    For some real fun you could make a function that does this your self by counting decimal places, multiplying that number ten, and then multiplying that by the number in the character variable (use the ASCII values to convert to real numbers). Just add all those numbers up and you'll get the answer.

    Or if you're referring to converting the binary number stored in a char variable to an integer, just use typecasting.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Why is atoi() not working for you? You could also try strtol().

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I assume your problem is that you have a single char instead of a string of chars, is that right? If so, a quick and dirty solution is to subtract '0' from your char, the result being the int.
    Last edited by jlou; 10-07-2004 at 09:41 PM. Reason: typo

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If that's the case, do typecasting. That's exactly what it was designed for and makes for more understandable code.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    do you even need to convert char to ints? Sometimes char can be used like ints. For example the following compiles with output as indicated using MSVC 6.0:
    Code:
    char ch = 'A';
    
     cout << ch + 10 << endl;
     
     for(int i = 0; i < 3; ++i)
    	cout << ch++ << endl;
     
    output:
    75
    A
    B
    C

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by sean_mackrory
    If that's the case, do typecasting. That's exactly what it was designed for and makes for more understandable code.
    If you were referring to my post, then that wouldn't work. A typecast would merely give the character's ASCII value, but subtracting '0' would convert it to the number it represents (assuming the character is a digit).

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Oh, yeah?
    Example program (Not the program that this is going to be used in)
    Code:
    int main()
    {
    char digit = '3';
    int dgt = digit-0;
    cout<<dgt<<endl<<flush;
    cin.get();
    return 0;
    }
    Output:
    Code:
    51
    What I want to do is make the integer 3. Atoi does this only for strings and not characters. Hmm... Sean, would you mind explaining your first post a bit more?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try

    digit - '0'

    rather than

    digit - 0

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Thanks! It worked and now I'm happy! Thy program I shalt make shalt work and shalt work fluently. LOL, old english.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by elad
    try

    digit - '0'

    rather than

    digit - 0
    I know what your getting at there, essentially, your subtraction the value of the char '0'.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    yup, just demostrating what jlou had indicated in an earlier post.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Ahh.... subtracting the character '0'. I see. Yeah that makes sense. I thought you were referring to subtracting 0 from the char since typecasting would occur automatically in the background, and placing the resulting integer value into the destination variable.

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    > Ahh.... subtracting the character '0'.
    Yeah, it looks like I confused several people with that point. I'll be more clear next time.

    > Oh, yeah?
    yeah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2009, 03:17 PM
  2. joining two chars together and converting to INT
    By tehprince in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 02:08 PM
  3. converting ints to chars
    By e66n06 in forum C Programming
    Replies: 4
    Last Post: 07-28-2007, 03:52 PM
  4. Converting Chars to Ints
    By sycorax in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2005, 10:40 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 11:33 AM