Convert a character to integer (ex '1' to 1)

This is a discussion on Convert a character to integer (ex '1' to 1) within the C++ Programming forums, part of the General Programming Boards category; Hello, I need to convert a character to an integer. How do I do this. For instance, if I have ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Convert a character to integer (ex '1' to 1)

    Hello,

    I need to convert a character to an integer. How do I do this. For instance, if I have a character '5', how do I get the integer 5?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    Code:
    char x = '5';
    int y = x - '0'; /* hooray! */
    .sect signature

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Thanks, but before you replied I wrote my own function

    Code:
    #include "cptd.h"	//include the header
    
    //begin the character pointer to double string
    double cptd(char * theString)
    {
    	double accumulator = 0;
    	double multiple = 10;
    
    	//for each character in the string
    	int length = strlen(theString);
    	for(int d = length, m = 0; d > 0; d--, m++)
    	{ 
    		//perform a shift on the current element
    		int curChar = (theString[d-1] & 0x0f);
    		accumulator += curChar * pow(10, m);
    	
    	}	//end for each character in the string
    
    	return accumulator;	//return the result
    
    }	//end the character pointer to double string
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <iostream>
    #include <cstdio>
    
    using namepspace std;
    
    int main( void ) {
        
        int aNumber;
        
        sscanf( "32", "%d", &aNumber ); // use %ld for long, %f for float, and %lf for double
        
        cout<<aNumber + 1<<endl;
    
        return EXIT_SUCCESS;
    
    }
    Would output 33.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    VanJay011379, I do remember a time when I would write functions such as yours (mostly because I was a learning and what better way to learn than to write your own functions) but you may want to start looking through the standard library before resorting to writing your own stuff.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    atoi()

    use atoi().

    example
    char buff;
    int i = atoi(buff);

    then you can do whatever you want~.....

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Write a program that takes say 1000 numbers and converts them. Log the start time , use your routine to convert them, then log the end time, now repeat with some of the other versions offered here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>example
    >>char buff;
    >>int i = atoi(buff);
    No, this is wrong, atoi() expects a char array, nul terminated. Check the FAQ for examples of doing this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Here's another way of doing it, using templates and stringstreams:
    Code:
    #include <iostream> // std::cout, std::endl
    #include <sstream>  // std::istringstream
    #include <iomanip>  // std::setprecision
    
    template <typename T>
    T to_number(const std::string& value)
    {
        std::istringstream is(value);
        T number;
        is >> number;
        return number;
    }
    
    int main()
    {
        int i = to_number<int>("123");
        float f = to_number<float>("123.456");
        double d = to_number<double>("1234.56789");
        std::cout << "int: " << i << "\n"
                  << "float: " << f << "\n"
                  << "double: " << std::setprecision(10) << d 
                  << std::endl;
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    37
    for a digit get the ascii value of the digit and subtract the ascii value for zero.

    e.g.

    ascii '4' = 52
    ascii '0' = 48
    ascii '9' = 57

    so for '4' -> 52 - 48 = 4
    and for '9' -> 57 - 48 = 9

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    Originally posted by ggs
    Code:
    char x = '5';
    int y = x - '0'; /* hooray! */

    hello,

    what the hell happened here? does this really work on any platform?

  12. #12
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by mickey
    hello,

    what the hell happened here? does this really work on any platform?
    yup, of course! in the ascii character set, all 10 decimal digits are adjacent and in the proper order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Replies: 3
    Last Post: 04-04-2002, 04:27 PM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 04:03 PM

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