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].
Related
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.
This question already has answers here:
Convert each digit from number saved in string to array of int
(2 answers)
Closed 5 years ago.
I have a number as a string. For example:
str_num = "12345"
So,
str_num[0] = 1
str_num[1] = 2
str_num[2] = 3
str_num[3] = 4
str_num[4] = 5
But
str_num[0] + str_num[1] + str_num[2] + str_num[3] + str_num[4] = 255
I want the result to be the sum of all the digits in the string. In this case that is:
str_num[0] + str_num[1] + str_num[2] + str_num[3] + str_num[4] = 15
How do I do that?
str_num[0] - '0' + str_num[1] - '0' + ...
is one way, i.e. subtract 5 lots of '0' from your current total. The subtraction of '0' is idiomatic in C and C++ as both standards insist that any encoding of digits has them in consecutive order and in a contiguous block.
This question already has answers here:
How does C Handle Integer Literals with Leading Zeros, and What About atoi?
(8 answers)
Closed 8 years ago.
Why does:
int test() {
return 00101 % 10;
}
return 5, while:
int test() {
return 101 % 10;
}
returns 1? I can't think of an explanation.
Integer literals beginning with 0 like
00101
is actually an octal constant.
00101 is octal value which is 65 in decimal so it returns 5.
00101 is in octal which is equal to 65 in decimal, so that is why the modulus operator will always give us 5.
You can do octal to decimal converstion on this link http://www.rapidtables.com/convert/number/octal-to-decimal.htm
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can i convert an int below 10 to a char for example :
5 -> '5'
(convert int to char without using ASCII table)
Since digits are always consecutive in a standard character set, you can write:
int number = 5;
int character = number + '0';
/* Here, character == '5' */
See, for instance, C11 standard.
n1570, § 5.2.1 Character sets
The 10 decimal digits: 0 1 2 3 4 5 6 7 8 9
[...]
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
The same applies in C++.
n3337, § 2.3 Character sets
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
If it is number 0-9 it is good to use:
int i = 5;
char ch = i + '0';
But probably the best option is to use itoa()
int i = 124;
char buffer[33];
itoa(i, buffer, 10); //10 mean decimal.
Here's an option
char c;
int x;
//...
switch ( x )
{
case 1:
c = '1';
break;
//and so on
}
And another:
std::map<int, char> mapping;
mapping[1] = '1';
//...
char c = mapping[4];
I'm not sure I understand what you mean by "without using ASCII table", but
int i = 5;
char c = i + '0';
will do what you want. The character codes for '0' through '9' are guaranteed to be consecutive and in the proper order.