Say I wanted to shift the letter 'A' into the letter 'D'. I can't seem to find anything that shows how to do that in C++ 17. Any suggestions?
Try just adding 3 to the character:
char myChar = 'A';
char shifted = myChar + 3; // shifted is now 'D'
Just treat each character like an integer and shift ‘A’ according to its ASCII value. This works because in c++ characters are encoded as 7-bit integers.
http://www.asciitable.com
Looking at the table we see that ‘A’ = 65 and ‘D’ = 68. So to shift ‘A’ to ‘D’, simply add 3 to ‘A’ like so:
char a = 'A';
a += 3;
std::cout << a;
Output:
D
char letter_A='A';
char letter_D=letter_A+3;
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I'm new to programming and sometimes see expressions like this
...
for (int i=0; i<str1.length(); i++)
{
int sum = ((str1[i]-'0')+(str2[i]-'0'));
str.push_back(sum%10 + '0');
}
...
So that is '0' here? Is it some kind of converting or something?
It's literally the character zero '0'
The operation str[i] - '0' is used to convert the representation of a digit into its numeric value.
Since the characters from '0' to '9' are following in the ascii table, having respectively, the values 48 to 57, when you perform the operation '3' - '0', the interpreter will use the ascii values, 51 - 48 == 3, so you can convert '3' to 3
ASCII Table
(source of the picture : Wikipedia)
'0' is the character zero. Since characters are sequential, adding a digit to 0 will produce the character representing that digit (once cast back to a char). E.g., (char)(2 + '0') (i.e., the integer two plus the character zero) will produce '2' (i.e., the character two).
Well
str2[i] - '0'
Converts character representation of a digit ('3', '7', '9') into its integer value (3, 7, 9). More accurate (and more wordy) construction is
(int)char.GetNumericValue(str2[i])
What is implied, but not really stated, in the other answers is that int and char can be treated as fairly equivalent in c#. You can do math on chars just like you can on ints, and you can convert back and forth between int and char with ease. The number you get is based on the position in the utf8 character tables for the char.
'0' as a char has an int value of 48, so you could do:
int x = 7;
char c = x+48; //c would be '7' as a char, or 55 as an int
Other examples:
char c = 'a';
c++;
Console.Write(c); //prints 'b', because 'a' + 1 is b
It's quite logical and reasonably helpful sometimes* but the main reason you might see '0' is that it's easier to remember '0' than it is to remember 48 (it's slightly easier to remember the hex version 0x30)
All these give you char 5 from int 5:
char five = 5 + 48;
char five = 5 + 0x30;
char five = 5 + '0';
Which one would you find easiest to remember? :)
*for example, say you wanted to count the chars in an ascii string, you could do:
var counts = new int[256];
foreach(char c in string)
counts[c]++;
You can use the char to index the array just like you can an int. At the end of the operation "hello world" would have put a 1 in index 104 (the h), a 3 in index 108(the l) etc..
Sure these days you might use a Dictionary<char, int> but appreciating that intrinsic char/int equivalence and how it can be used has its merits..
What is '0' means?
In C++, it is a character literal.
The fundamental reason why/how str1[i] - '0' works is through promotion.
In particular when you wrote:
str1[i]-'0'
This means
both str1[i] and '0' will be promoted to an int. And so the result will be an int.
Let's looks at some example for more clarifications:
char c1 = 'E';
char c2 = 'F';
int result = c1 + c2;
Here both c1 and c2 will be promoted to an int. And the result will be an int. In particular, c1 will become(promoted to) 69 and c2 will become(promoted to) 70. And so result will be 69 + 70 which is the integer value 139.
If I want to compile the following code:
int x = 8;
int y = 17;
char final[2];
final[0] = (char) x;
final[1] = (char) y%10;
cout << final[0] << final[1] << endl;
It shows nothing. I don't know why? So how can I successfully convert it?
(char)8 is not '8', but the ASCII value 8 (the backspace character). To display the character 8 you can add it to '0':
int x = 8;
int y = 17;
char final[2];
final[0] = '0' + x;
final[1] = '0' + (y%10);
cout << final[0] << final[1] << endl;
As per your program you are printing char 8, and char 7.
They are not printable. In fact they are BELL and Backspace characters respectively.
Just run you program and redirect it to a file.
Then do an hexdump, you will see what is printed.
./a.out > /tmp/b
hd /tmp/b
00000000 08 07 0a |...|
00000003
What you need to understand is that in C++, chars are numbers, just like ints, only in a smaller range. When you cast the integer 8 to char, C++ thinks you want a char holding the number 8. If we look at our handy ASCII table, we can see that 8 is BS (backspace) and 7 is BEL (which sometimes rings a bell when you print it to a terminal). Neither of those are printable, which is why you aren't seeing anything.
If you just want to print the 87 to standard output, then this code will do that:
cout << x << (y%10) << endl;
If you really need to convert it chars first, then fear not, it can still be done. I'm not much of a C++ person, but this is the way to do it in C:
char final[2];
snprintf(final, sizeof final, "%d%d", x, y % 10);
(See snprintf(3))
This code treats the 2-char array as a string, and writes the two numbers you specified to it using a printf format string. Strings in C normally end with a NUL byte, and if you extended the array to three chars, the final char would be set to NUL. But because we told snprintf how long the string is, it won't write more chars than it can.
This should also work in C++.
Brian Kernighnan in his book Programming with C says
By definition, chars are just small integers, so char variables and
constants are identical to ints in arithmetic expressions.
Does this mean we can subtract char variable from int ??
I wrote a small piece of code:
#include <stdio.h>
main()
{
int a ;
int c;
a = 1;
c = 1 - '0' ;
printf("%d", c);
}
However it gives me output = -47...
What is that I'm doing wrong ?? Are the variables I assigned have the right type??
The output is to be expected. '0' is a char value that, since your compiler presumably uses the ASCII encoding, has value 48. This is converted to int and subtracted from 1. Which gives the value -47.
So the program does what it is expected to do, just not what you might hope it would do. As for what you are doing wrong, it is hard to say. I'm not sure what you expect the program to do, or what problem you are trying to solve.
The characters from '0'-'9'' have values 48-57 when converted to integer ('0' = 48, '1' = 49 etc). Read more about ASCII Values. When used in numerical calculation, first they are converted to int, so 1- '0' = 1-48 =-47.
You're mixing here the actual operation with the form of representation. printf outputs the data according to the specified format - integer in your case. If you want to print it as a character, switch %d with %c.
What you are doing is treating with the ASCII code of the chars, each char has an ASCII value assigned.
Now, playing a little with the ASCII of each char you can do things like:
int a;
a = 'a' - 'A' ;
printf("%d", a);
And get 32 as output, due to the ASCII value to 'a' = 97 and for 'A' = 65, then you have 97-65 = 32
I think this gives you more clear understanding...
#include <stdio.h>
main()
{
int a ;
a = 1;
printf("%d", char(a+48));
//or printf("%d", char(a+'0'));
}
I cannot figure out why this result in undefined behavoiur or whatever its called?
char ch[10];
strcpy(ch, "street1-3");
char ch2 = ch[6];
cout << ch2 << endl;
int n = (int) ch2;
n = n * 12;
cout << n << endl;
that is cout first prints out position 6 of street1-3 that is "1".
Then when I am trying to typecast this char to an int the value becomes not what its expected to be, that is 12, instead 588.
output in the consolewindow
1
588
What I am doing wrong in the typecasting and how is it solved?
The character '1' doesn't have the ascii value of 1. So (int)'1' != (int)1.
If you look at an ASCII table, you'll see that '1' == 49, and coincidentally (or not...) 49*12 == 588.
There is 2 different behaviour for the operator << on stream with char and int. With char, you get the corresponding character, i.e. "1", wich has a ascii value of 49. So when you do int n = (int)ch2, n has a value of 49, then n * 12 is equal to 588 which is outputed as a value because n is a int.
AS ch2 = 1 , which is of type 'char' , when it is type cast into 'int' , it takes ASKII value of 1 which is 49 and assigns it to 'n' . And 49 * 12 is 588 which is output of your program .
(int)ch2 tells the compiler to interpret the char value in ch2 as an int. Since characters are stored by ascii value, ch2 contains 49, which is what the calculation uses.
Instead of (int)ch2 you should instead use int(ch2), which will perform the conversion you expect.
Oh, and you should always avoid c style casts in c++ where possible - c++ provides a number of safer casts that you should use instead (dynamic,static,etc).
Im able to convert most things without a problem, a google search if needed. I cannot figure this one out, though.
I have a char array like:
char map[40] = {0,0,0,0,0,1,1,0,0,0,1,0,1... etc
I am trying to convert the char to the correct integer, but no matter what I try, I get the ascii value: 48/ 49.
I've tried quite a few different combinations of conversions and casts, but I cannot end up with a 0 or a 1, even as a char.
Can anyone help me out with this?
Thanks.
The ascii range of the characters representing integers is 48 to 57 (for '0' to '9'). You should subtract the base value 48 from the character to get its integer value.
char map[40] = {'0','0','0','0','0','1','1','0','0','0','1','0','1'...};
int integerMap[40];
for ( int i = 0 ;i < 40; i++)
{
integerMap[i] = map[i] - 48 ;
// OR
//integerMap[i] = map[i] - '0';
}
If the char is a literal, e.g. '0' (note the quotes), to convert to an int you'd have to do:
int i = map[0] - '0';
And of course a similar operation across your map array. It would also be prudent to error-check so you know the resulting int is in the range 0-9.
The reason you're getting 48/49 is because, as you noted, direct conversion of a literal like int i = (int)map[0]; gives the ASCII value of the char.