Alex Rivera | Logout

Convert a character digit to the corresponding integer in C

Asked 2009-03-10T02:53:12.330
139

Is there a way to convert a character to an integer in C?

For example, from '5' to 5?

Edit
Report

1 Answer

0

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.

answered 2009-03-10T03:05:54.967

Your Answer