Is there a way to convert a character to an integer in C?
for example, '5' -> 5
thanks.
Is there a way to convert a character to an integer in C?
for example, '5' -> 5
thanks.
Subtract '0' like this:
int i = c - '0';
The C Standard guarantees each digit in the range '0'..'9'
is one greater than its previous digit (in section 5.2.1/3
of the C99 draft). The same counts for C++.
char numeralChar = '4';
int numeral = (int) (numeralChar - '0');
As per other replies, this is fine:
char c = '5';
int x = c - '0';
Also, for error checking, you may wish to check isdigit(c) is true first. Note that you cannot completely portably do the same for letters, for example:
char c = 'b';
int x = c - 'a'; // x is now not necessarily 1
The standard guarantees that the char values for the digits '0' to '9' are contiguous, but makes no guarantees for other characters like letters of the alphabet.
You would cast it to an int (or float or double or what ever else you want to do with it) and store it in anoter variable.
If it's just a single character 0-9 in ASCII, then subtracting the the value of the ASCII zero character from ASCII value should work fine.
If you want to convert larger numbers then the following will do:
char *string = "24";
int value;
int assigned = sscanf(string, "%d", &value);
** don't forget to check the status (which should be 1 if it worked in the above case).
Paul.
char chVal = '5';
char chIndex;
if ((chVal >= '0') && (chVal <= '9')) {
chIndex = chVal - '0';
}
else
if ((chVal >= 'a') && (chVal <= 'z')) {
chIndex = chVal - 'a';
}
else
if ((chVal >= 'A') && (chVal <= 'Z')) {
chIndex = chVal - 'A';
}
else {
chIndex = -1; // Error value !!!
}
If, by some crazy coincidence, you want to convert a string of characters to an integer, you can do that too!
char *num = "1024";
int val = atoi(num); // atoi = Ascii TO Int
val
is now 1024. Apparently atoi()
is fine, and what I said about it earlier only applies to me (on OS X (maybe (insert Lisp joke here))). I have heard it is a macro that maps roughly to the next example, which uses strtol()
, a more general-purpose function, to do the converstion instead:
char *num = "1024";
int val = (int)strtol(num, (char **)NULL, 10); // strtol = STRing TO Long
strtol()
works like this:
long strtol(const char *str, char **endptr, int base);
Converts *str
to a long
, treating it as if it were a base base
number. If **endptr
isn't null, it holds the first non-digit character strtol()
found (but who cares about that).
Hi!
is there a way that i can check if the input is either a int or a char?
thanks,
When I need to do something like this I prebake an array with the values I want.
const static int lookup[256] = { -1, ..., 0,1,2,3,4,5,6,7,8,9, .... };
Then the conversion is easy
int digit_to_int( unsigned char c ) { return lookup[ static_cast<int>(c) ]; }
This is basically the approach taken by many implementations of the ctype library. You can trivially adapt this to work with hex digits too.