Converting a char to an int in c++ is a common operation when sorting a char array or storing numbers in a char array. A char is a pointer to a char data type or a string of char data types. Char data types can be any English characters (e.g. a through z) or any ASCII character. An int is a data type that can hold integers, i.e. whole numbers.
Place your char, we'll call it pcMyCharStar, into the condition section of an "if" statement. This verifies that your char is pointing to a char or a string of chars, and therefore does not equal nil.
if( pcMyCharStar )
// Your pcMyCharStar does NOT equal to nil.
// Your pcMyCharStar does equal nil.
Access the data stored in the char by placing the de-referencing operator to the left of our char . e.g. char myChar= pcMyCharStar;
This operation gives access to the data the pointer was referencing. Another way to do this is by treating the char* as an array and using the "[ ]" operator.
e.g. char myChar= pcMyCharStar;
This operator returns the first element or the only element in the char* array.
Typecast the data accessed in step two into an int by placing the data type you are casting to in between parenthesis to the left of the variable you are casting. e.g.
int nMyInt= (int)myChar;
Typecasting is the process of changing a variable from one data type to another, in this case, from a char to an int.
- Photo Credit Stockbyte/Stockbyte/Getty Images
How to Convert Char to Int in an Access Query
Microsoft Access has a function that allows you to convert any value, including a character, into an integer. This works by converting...
How to Convert a Word to an Integer
Programming often requires converting data from one type to another. A word is usually stored as a string or character array. Users...
How to Convert a String to an INT C
In "C," character strings are stored as an array of character codes and manipulated as characters. If your character strings represent whole...
How to Convert an Integer to a Character in C++
Converting an integer to its ASCII equivalents is a common practice in programming. For many functions, it is necessary to manipulate the...
How to Convert From String to Char in C++
Handling text is an important aspect of software development. The C++ language has two methods of handling text: C-Style strings and C++...