Why in the ASCII table Russian letter ‘a’ is equal to 160, and when I write:
int a = 'a';
COUT & LT; & LT; A & LT; & LT; Endl;
I displays -32?
Answer 1, Authority 100%
The result of the execution of your code depends on several factors. In particular, from the source encoding, the compiler used and the symbolic and string literal coding rules.
For example, it is possible to output the number of 53424
and Warnings:
Multi-Character Character Constant [-Wmultichar]
When using UTF-8
Source encodings and Compiler GCC
. That. 'A'
This is not at all a symbol, but an integer (in more detail about multisim political literals you can read here ), what can be seen by checking Sizeof
, which return the same meaning as for int
, and not for char
(by definition 1
).
When using Clang
I generally get Error Compilation:
Character Too Large for Enclosing Character Literal Type
To get rid of such an error you have to change the type of character literal (for example, WCHAR_T
, setting the prefix L
). In this case, we already obtain another number: 1072
What corresponds to UTF-16
Code of the Russian letter And
.
To get the same result as you, but using the GCC
compiler, you can use the -FEXEC-CHARSET
with a code page 1251
.
At the same time, I want to note that for the letter a
code 160
(0xa0
) corresponds to the encoding CP866 , and not CP1251 , in which it is 224
(0xe0
). Therefore, -32
in 160
will not be able to turn into a conventional conversion of the iconic number to an unsigned.
If you really want to get 160
in addition to the conversion to an unsavage type, it is necessary to use the CP866 codeping page. Those. Collect the following code with a key -fexec-charset = cp866
:
# include & lt; iostream & gt;
INT MAIN ()
{
int a = static_cast & lt; unsigned char & gt; ('a');
STD :: COUT & LT; & LT; A & LT; & LT; STD :: ENDL;
}
Answer 2, Authority 80%
Because the number of more than 127 does not fit into the sign char
, which has a range from -128 to 127. But it climbs into an unsaved, but, apparently, on your system CHAR
is a sign, so conversion from the icon char
in the sign int
. Because Any number Char
always commits in int
, it turns out that int
gets a negative number from char
To get a positive number, just use any unsigned
type as a receiver value, and the source char
Convert to Unsigned Char
:
unsigned int a = static_cast & lt; unsigned char & gt; ('A');