Parse elements of a std::string into integers (C++) [duplicate] - c++

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 2 years ago.
I am getting an odd error in my code where I try to take the elements of a std::string and convert them into 2 seperate ints
An example of the string would be "A6". I would like to be able to convert this string into 2 integers. In this example, the integers would be 65 (because 'A' is 65 in the ASCII chart) and 6.
Currently this is the code:
// Parse string into integers
int tempRow = userGuess[0];
int tempColumn = userGuess[1];
std::cout << tempRow << tempColumn;
"A1" outputs 65 and 49.
Why does '1' become an integer of 49?

The ascii code for 1 is 49, which is the result you are assigning to tempColumn. If you want the integer value, you need to do:
int tempColumn = userGuess[1] - '0';
This subtracts the ascii version of 0 which is 48 from the integer.

Related

how to store 0 in MSB of int datatype in C++? [duplicate]

This question already has answers here:
Print leading zeros with C++ output operator?
(6 answers)
Closed 12 months ago.
#include<iostream>
using namespace std;
int main()
{
int x = 0101;
cout<<x;
return 0;
}
The output I am getting is 101 but I want 0101 instead. what to do??
First of all, you should get 65 as 0101 is parsed as octal 101 (64+1).
If you want to use binary literal, you can prepend 0b
#include<iostream>
using namespace std;
int main(){
int x = 0b0101;
cout<<x;
return 0;
}
https://godbolt.org/z/5Pxqehz7P
Integer values are written in
4 forms in C++:
Decimal with digits 0-9: 1590, 4581
Octal with digits 0-7 and starting with a leading zero: 043, 077
Hexadecimal with digits 0-9 and a-f, starting with a leading 0x or 0X: 0xc0d3, 0X170
Binary from C++ version 14 onwards with digits zero and one, a leading 0b or 0B: 0b1011, 0B0101
You number (a.k.a numeric literal) 0101 is in octal form (in standard C++ anyway) as it has a leading zero.
As #peru mentioned, you have to convert your number into one of the four supported numeric literal forms mentioned above.
If you have a C++14 compiler, you can do 0b0101 directly. Chances are you have to stick with the other three forms.
Octal and hexadecimal are easier to convert to and from binary. It's a good idea to choose them when working with binary. Five is still five in octal and hexadecimal, of course...
The cout stream prints integers in decimal format. So you can look at your compiler documentation to see if there is a binary output option. Alternatively, you can implement it yourself. For an 8 bit number:
void print_bin8(int num){
for(int pos = 7; pos > 0; pos--){
int bitmask = (1 << pos);
int bit = num & bitmask;
if(bit != 0){ cout << 1;}
else{ cout << 0;}
}
Basically mask each bit out with an AND and print it MSB to LSB.

Take a digit of a string as an integer in C++ [duplicate]

This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 2 years ago.
I want to add the first digit of a string 111 to the integer x = 0 so that it equals
x = 0 + 1 = 1
The following code takes the character 1 instead of the integer 1:
int x = 0;
string str = "111";
x += str[1];
std::stoi did not work either:
x += std::stoi(str[1]);
The simple way to convert a digit to an integer is to substract '0' from it.
x += str[0] - '0';
This works because the encodings of the decimal digits are guaranteed to be continuous. So subtracting the lowest digit gives you the digit value.
Your other error is that the first character of a string is str[0] not str[1].

Why is '1' + '1' = 98 and '1' + 1 = 50? [duplicate]

This question already has answers here:
Sum of two chars in C/C++
(7 answers)
Closed 4 years ago.
I'm coming from high language, PHP js and things. So this seem strange to me.
I'm using either local or online interpreter but I always get this result.
I suppose this result is because '2' is 50 in ASCII and 98 is 'b' but I'm not sure. Also I don't really understand how the conversion work.
The code is here:
#include <iostream>
#include <string>
int main()
{
std::cout << '1' + 1 << '\n';
std::cout << '1' + '1' << '\n';
}
Type char is integral type. Each character maps to an integer value. The value depends on the encoding used which in your case is probably ASCII. So the character '1' probably has an integer value of 49 thus the '1' + '1' expression is equivalent to 49 + 49 and results in 98. Adding integer value of 1 to 49 results in 50. Which is the same as adding integer value of 1 to (a value represented by the) character '1'.
In a nutshell, values are values, whether represented via character literals or integer literals.
'1' is a char constant with a specific value determined by the encoding used on your system. That encoding might be ASCII, but it might not. When used as an argument to +, it is promoted to an int. So decltype('1') is a char, but decltype('1' + '1') is an int.
On your system, it's clear that '1' has the value 49. That's why'1' + '1' is 98. And therefore '1' + 1 is 50.
Note that in C, '1' is an int type. Arguably that's less confusing than the way C++ has it.

Understanding how to convert char[0] to int [duplicate]

This question already has answers here:
How does subtracting the character '0' from a char change it into an int?
(4 answers)
Closed 8 years ago.
I was trying to go in a loop and each time convert a character in a string to it's integer value and I don't mean the ASCII value. Tried to use atoi() with no luck but then I stumbled upon this question Convert single char to int and my code worked. The code is as follows:
std::string tmp = "87532621";
for(i=0;i<tmp.length();i++)
{
**int num = tmp[i] - '0';**
//do some processing
}
I fail to understand why the following line of code works. My question is how is it converting the char value to integer type?
int num = tmp[i] - '0';
Each char in your string is an ascii value. The ascii values are just 7 bit numbers.
The numerical values for the character digits lies in a sequence 0123456789 which is very convenient because it makes it possible to write
int zero = '0' - '0'; // 0 (zilch)
int one = '1' - '0'; // one (1)
int nine = '9' - '0'; // 9 (three times three)
And so on.
The actual numerical values are not important for this to work. The fact that the are next to each other in the character set is.
See wikipedia - ascii for the actual numerical values.

How do I convert a char into int in C++? [duplicate]

This question already has answers here:
How would I convert a char array to int? [closed]
(2 answers)
Closed 8 years ago.
I have created an Array of type char.
In that Array, I am accepting integers.
Like 0, 1 or anything for that matter.
The question asks for summation of values stored into the Array. But since I have declared the array as char type, so 0 would be accepted like '0'. SO, How should I convert '0' to 0.
Also, Can characters greater than 10(ten) also be converted into integers ?
The input is a string of integers; like 00012312.
Also is there a better method of inputting these numbers apart from this generic character Array method.
Thanks in advance for your help.
If you have a character array like this
char a[] = "00012312";
then you can find the sum the following way
int sum = 0;
for ( char *p = a; *p; ++p ) sum += *p - '0';
If you need to convert the content of the array to an integer then you can write
int x = ( int )std::strtol( a );
It is the same as
int x = std::atoi( a );
A char containing 0...9 can be converted to an int by subtracting '0'. Study an ASCII chart to understand why.
char c = '7';
int n = c - '0';