Changing '7' to 7 (char to int)?

I know this is probably pretty basic and makes you wonder why I didn't look it up, but I did look it up. Unfortunately, everything I found was how to change a char to it's ASCII value.

So, how can I change '7' (a char), for example, to 7 (an int)?

Help would be appreciated, thanks!


Edit: I know how to change a string to an int. However, changing a char to a string then to an int doesn't seem to work.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
  char ex = '5';
  int n;
  
  string str = to_string(ex);
  n = stoi(str);
  cout << n;
}


Output: 53
Last edited on
Subtract '0' (the character zero) from '7' to yield the int 7.

static_assert(7 == '7' - '0');

This will work because it is guaranteed that the character codes representing the digits 0-9 are contiguous and in order. There is no such guarantee for any other group of characters. In particular, the character codes for the letters a-z and A-Z don't necessarily have this behavior, so you can't assume in general e.g.
static_assert(2 == 'c' - 'a');
Although that assertion is usually true.
Last edited on
this worked fine for me.

int main()
{
string s;
s += '7';
cout << stoi(s);
}

but I don't know if that can get confused by some locale settings.

I thought about a lookup table but a subtraction is probably the same internal effort.

the subtraction is the way to go, but it is possible in the string.
Last edited on
Line 7: The internal representation of '5' is 53.

Line 10: ex is promoted from a char to an int. 53 is passed to to_string().

Perhaps the easiest solution is to change ex to a C-string or a std::string.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
{
    const char ex[] = "5";    // C-string
    int n;

    n = stoi(ex);
    cout << n << endl;
}



Last edited on
easier than my += with std::string? Its virtually identical, apart from c string vs c++ string?
the issue looks like tostring. Regardless -'0' is the most efficient, involving data structures and functions is just overhead.
Last edited on
Thank you all for replying!

Apologies for the very late reply. I have a ton I need to get done before I can work on this, but I'll look into each of these, I appreciate it!
<cstdlib> has functions to convert C strings to numeric types. They take C string char arrays as parameters, as well as char variables. For char variables you pass the address of the variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdlib>
#include <string>

int main()
{
   // create a C string
   char str[] = "123";

   // convert C string to int
   int str_int = atoi(str);

   std::cout << str << ' ' << str_int << "\n\n";

   // create a char variable
   char seven_char = '7';

   // convert the char variable to an int,
   // passing the char variable's address
   int seven_int = atoi(&seven_char);

   std::cout << seven_char << ' ' << seven_int << "\n\n";

   // create a std::string
   std::string seven_str;
   
   // assign the std::string a value
   // (implicit conversion of char to std::string)
   seven_str = '7';

   // convert the string to an int
   seven_int = std::stoi(seven_str);

   std::cout << seven_str << ' ' << seven_int << "\n\n";

   str_int = std::stoi(str);

   std::cout << str << ' ' << str_int << '\n';
}
123 123

7 7

7 7

123 123
Last edited on
Furry Guy wrote:
<cstdlib> has functions to convert C strings to numeric types. They take C string char arrays as parameters, as well as char variables. For char variables you pass the address of the variable.

...

Thank you for your response too! I see what I did wrong while trying to use stoi and I've fixed it. I've also bookmarked this page for future reference of the various ways this can be solved. :)

This post is closed / solved now.
I see what I did wrong while trying to....

You learned something valuable, there is more than one way to muck around with strings/char arrays. :)
Topic archived. No new replies allowed.