Converting a character representation of an integer, to an integer value

This is a discussion on Converting a character representation of an integer, to an integer value within the C Programming forums, part of the General Programming Boards category; I am quite new to programming (this will become obvious as you read on.. lol) and I need to make ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    Converting a character representation of an integer, to an integer value

    I am quite new to programming (this will become obvious as you read on.. lol) and I need to make a function that will take the character representation of a number (ie. '3'), and return it's integer value (ie 3).

    I did this using a switch/case statement, and it works fine. However, it seems that it might be pretty inefficient. This function is going into firmware that is loaded on to a 8051 family processor, so I need it to follow C rules strictly, and to be as efficient as possible.

    Any suggestions would be appreciated!

    Here is the code I created:
    Code:
    int char2int(char ch)
    {
    	int number;
    	
    	switch((int)ch)
    	{
    		case 48: number = 0;
    			break;
    		case 49: number = 1;
    			break;
    		case 50: number = 2;
    			break;
    		case 51: number = 3;
    			break;
    		case 52: number = 4;
    			break;
    		case 53: number = 5;
    			break;
    		case 54: number = 6;
    			break;
    		case 55: number = 7;
    			break;
    		case 56: number = 8;
    			break;
    		case 57: number = 9;
    			break;
    		default: number = -1;
    	}	
    	
    	return number;
    }
    Thanks in advance!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    char x = '3';
    return x-'0';
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Quote Originally Posted by vart View Post
    char x = '3';
    return x-'0';
    Ooh.. tricky.. LOL.. I like that.
    So simple that I overlooked it!

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by RATyson View Post
    Ooh.. tricky.. LOL.. I like that.
    So simple that I overlooked it!
    Suppose you always fill your gas tank by drilling a hole in the side of your car and sticking a hose in there. If somebody showed you how to use the gas cap, would you say "Ooh, tricky?"

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Quote Originally Posted by brewbuck View Post
    Suppose you always fill your gas tank by drilling a hole in the side of your car and sticking a hose in there. If somebody showed you how to use the gas cap, would you say "Ooh, tricky?"
    Ha.. no. The first line of my response was sarcasm. As soon as I saw his post, It was immediately appearant how simple the solution was. It was right there in front of me the whole time and I looked right past it.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, to have a function that does exactly the same thing as your original function, you'd need to check to see if it was a digit first. The easiest (and most portable) way to do this is to use isdigit() from <ctype.h>:
    Code:
    #include <ctype.h>
    
    int char2int(int c) {
        if(isdigit(c)) return c - '0';
        else return -1;
    }
    Just subtracting '0' doesn't work if the character isn't a digit.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a character to integer (ex '1' to 1)
    By VanJay011379 in forum C++ Programming
    Replies: 11
    Last Post: 03-18-2003, 01:14 AM
  2. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  3. Placing an integer into a character
    By WebmasterMattD in forum C++ Programming
    Replies: 8
    Last Post: 09-05-2002, 08:49 AM
  4. Replies: 7
    Last Post: 07-22-2002, 03:36 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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