I am Kind Of New To C++. But I Want To Turn The Character 2 into An integer 2. I tried using Casting But It Gives Me The ASCII Value Of The Char. How Can I Achieve This Iam Using Linux And Codeblocks
Asked
Active
Viewed 5,135 times
-2
-
The referenced answer does not mention any form of formatted extraction. – 2785528 Aug 29 '20 at 16:19
2 Answers
8
Just subtract the ASCII value of '0'
from '2'
to get the integer 2
.
char c = '2';
int n = c - '0';
This is guaranteed to work even if the encoding is not ASCII, since the language requires that the encoding of the characters increases from '0'
to '9'
.

cigien
- 57,834
- 11
- 73
- 112
0
I Want To Turn The Character 2 into An integer 2 ...
C++ std::istream provides several stream operators. You probably should learn how to use "space delimited formatted extraction".
An example:
#include <iostream>
using std::cout, std::cerr, std::endl;
//using std::hex, std::dec, std::cin, std::flush; // c++17
#include <string>
using std::string;
#include <sstream>
using std::stringstream;
#ifndef DTB_PCKLRT_HH
#include "../../bag/src/dtb_pclkrt.hh" // private library
using DTB::PClk_t; // posix clock access
#endif
#include <cstdint>
#include <cassert>
namespace // anonymous
{
class F893_t // Functor ctor and dtor use compiler provided defaults
{
PClk_t pclk; // posix clock
public:
int operator()(int argc, char* argv[]) { return exec(argc, argv); }
private:
int exec(int , char** )
{
int retVal = 0;
uint64_t start_ns = pclk.ns();
// create a string -- white space delimits, but extra is ignored
string s = "1 2 3 4 55555 666666 ";
// use s to initialize a stringstream
stringstream ss(s);
do
{
int n; // this object will hold the extracted value
// NOTE: it is wrong to test eof() here, it is unknown.
ss >> n; // n also identifies target translation , i.e. an "int"
// the default translation format of "to int" is from 'decimal text'
// non decimal text can trigger a failure
// NOTE: ss status is not valid until AFTER a read attempt
if (!ss.good()) // check ss error status
{
if (ss.eof()) { break; } // attempt to read past the end of file is an error
// but typically, you don't want to fail your program because of it
// instead, simply continue after the loop
// getting here means that ss is not good() and not eof()
// This can be caused by an invalid char, and other errors
// so at least let the user know
// Test NOTE: to test "no eof()" and "not good"
// change string s to include a char that is not valid for decimal
cerr << "\n\n ss.bad() after 'n' formatted integer read attempt"
<< "\n ss.str(): " << ss.str() << endl;
assert(0); // it is possible to continue ...
// but not useful for this example
}
// at this point, n should have a value from the stringstream
cout << "\n n = " << n;
} while (true);
cout << "\n\n --eof()-- detected, resumed after loop" << endl;
auto duration_ns = pclk.ns() - start_ns;
cout << "\n F893_t::exec() duration "
<< duration_ns << " ns (" << __cplusplus
<< ")" << endl;
return retVal;
}
}; // class F893_t
} // anonymous namespace
int main(int argc, char* argv[]) { return F893_t()(argc, argv); }
// "Everything should be made as simple as possible, but no simpler"
// attributed to Albert Einstein the New York times in 1950
Typical output:
n = 1
n = 2
n = 3
n = 4
n = 55555
n = 666666
--eof()-- detected, resume after loop
F893_t::exec() duration 149825 ns (201709)

2785528
- 5,438
- 2
- 18
- 20
-
A stringstream inherits std::istream, as does iostream, cin, and fstream. so white space delimited formatted extraction works the same on all of them. – 2785528 Aug 29 '20 at 16:06
-
This example converts 4 different single digit chars into a decimal integer, including converting '2' into integer 2. It then demo's 2 multi digit integer conversions. – 2785528 Aug 29 '20 at 16:12