Dean R.
—You have a variable of type char
that actually represents an integer. For example; char three = '3'
. You need to convert this char
to an int
so that you can use mathematical operations on it (for example, to be able to execute three + 1
and get 4
as a result).
C and C++ store characters as integers using their underlying ASCII codes, so '0'
is 48, '1'
is 49, and so on. The easiest way to convert a single character to the integer it represents is to subtract the value of '0'
. If we take '3'
(ASCII code 51) and subtract '0'
(ASCII code 48), we’ll be left with the integer 3
, which is what we want.
You can do this in C as follows:
#include <stdio.h> int main() { char three_char = '3'; int three_int = three_char - '0'; int result = three_int + 1; printf("%c + 1 is %d\n", three_char, result); }
This will output:
3 + 1 is 4
Or, equivalently, in C++:
#include <iostream> using namespace std; int main() { char three_char = '3'; int three_int = three_char - '0'; int result = three_int + 1; cout << three_char << " + 1 is " << result << endl; return 0; }
Because char
is limited to a single character, this is only relevant for the numbers 0-9. If you have a number like “12” stored as a character array or string, you can use the strtol
(string to long) function that is part of stdlib
in C.
In C:
#include <stdio.h> #include <stdlib.h> int main() { char twelve_str[] = "12"; char *output; long result = strtol(twelve_str, &output, 10) + 1; printf("%s + 1 is %ld\n", twelve_str, result); }
In C++:
#include <iostream> using namespace std; int main() { char twelve_str[] = "12"; char *output; long result = strtol(twelve_str, &output, 10) + 1; printf("%s + 1 is %ld\n", twelve_str, result); }
The strtol
function takes an output pointer as its second argument. In the example above, we don’t need this, and we don’t use our output
variable, but in the case when you have an input string containing both numbers and other characters, the output variable will point to the first character after the parsed long.
The function also takes the base as its third argument, so we use 10
in the example above to indicate that the long should be parsed in base 10.
Note that this function will simply cast your variable to 0
in case of any problems. Therefore, the following code will print "twelve + 1 is 1"
, as it cannot cast the array “twelve” to an integer, and therefore does the sum 0 + 1
.
// BAD EXAMPLE #include <stdio.h> #include <stdlib.h> int main() { char twelve_str[] = "twelve"; char *output; long result = strtol(twelve_str, &output, 10) + 1; printf("%s + 1 is %ld\n", twelve_str, result); }
You can check for errors by looking at the value of the output variable. For example, if you’re expecting your entire input variable to be parsed into the long
, the following will print an error message if all or part of the input could not be parsed.
#include <stdio.h> #include <stdlib.h> int main() { char twelve_str[] = "twelve"; char *output; long result = strtol(twelve_str, &output, 10) + 1; if (*output) { printf("Error occurred"); } else { printf("%s + 1 is %ld\n", twelve_str, result); } }
Also note that the following functions are deprecated and should not be used. They are shortcut functions to strtol
above, which convert to long, integer, and float respectively, and do not require the output variable or the base to be specified (defaulting to base 10).
atol
atoi
atof
If you’re looking to get a deeper understanding of how C++ application monitoring works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.