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:
Thanks in advance!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; }