C++ 负 ASCII 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4690415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Negative ASCII value
提问by user963241
What's the point of negative ASCII values?
负 ASCII 值有什么意义?
int a = '?'; //a = -85 but as in ASCII table '<<' should be 174
回答by Lightness Races in Orbit
There are no negative ASCIIvalues. ASCII includes definitions for 128 characters. Their indexes are all positive (or zero!).
没有负的ASCII值。ASCII 包括 128 个字符的定义。他们的指数都是正的(或零!)。
You're seeing this negative value because the character is from an Extended ASCIIset and is too large to fit into the char literal. The value therefore overflows into the bit of your char
(signed on your system, apparently) that defines negativeness.
您看到此负值是因为该字符来自扩展 ASCII集并且太大而无法放入字符文字中。因此,该值会溢出到您的char
(显然已在您的系统上签名)定义负值的部分。
The workaround is to write the value directly:
解决方法是直接写入值:
unsigned char a = 0xAE; // ?
I've written it in hexadecimal notation for convention and because I think it looks prettier than 174
. :)
我已经用十六进制表示法编写了它,因为我认为它看起来比174
. :)
回答by ndim
This is an artefact of your compiler's char
type being a signedinteger type, and int
being a wider signed integer type, and thus the character constant is considered a negative number and is sign-extended to the wider integer type.
这是编译器char
类型是有符号整数类型和int
更宽的有符号整数类型的人工制品,因此字符常量被视为负数并被符号扩展为更宽的整数类型。
There is not much sense in it, it just happens. The C standard allows for compiler implementations to choose whether they consider char
to be signed or unsigned. Some compilers even have compile time switches to change the default. If you want to make sure about the signedness of the char
type, explicitly write signed char
or unsigned char
, respectively.
它没有多大意义,它只是发生了。C 标准允许编译器实现选择是否考虑char
有符号或无符号。一些编译器甚至有编译时开关来更改默认值。如果您想确定char
类型的签名,请分别明确地写出signed char
或unsigned char
。
Use an unsigned char
to be extended to an int
to avoid the negative int
value, or open a whole new Pandora's box and enjoy wchar
.
使用 anunsigned char
扩展为 anint
以避免负值int
,或者打开一个全新的潘多拉魔盒并享受wchar
。
回答by unwind
There is no such thing. ASCII is a table of characters, each character has an index, or a position, in the table. There are no "negative" indices.
哪有这回事。ASCII 是一个字符表,每个字符在表中都有一个索引或位置。没有“负面”指数。
Some compilers, though, consider char
to be a signed integral data type, which is probably the reason for the confusion here.
但是,一些编译器认为char
是有符号整数数据类型,这可能是造成此处混淆的原因。
If you print it as unsigned int
, you will get the same bits interpreted as a unsigned (positive) value.
如果将其打印为unsigned int
,您将得到解释为无符号(正)值的相同位。
回答by jv42
ASCII ranges 0..127, ANSI (also called 'extended ASCII') ranges 0..255.
ASCII 范围为 0..127,ANSI(也称为“扩展 ASCII”)范围为 0..255。
ANSI range won't fit in a signed char (the default type for characters in most compilers).
ANSI 范围不适合有符号字符(大多数编译器中字符的默认类型)。
Most compilers have an option like 'char' Type is Unsigned (GCC).
大多数编译器都有一个选项,如'char' Type is Unsigned (GCC)。
回答by SVGreg
I had this artifact. When you use char as symbols you have no problem. But when you use it as integer (with isalpha(), etc.) and the ASCII code is greater then 127, then the 'char' interpret as 'signed char' and isalpha() return an exception. When I need use the 'char' as integer I cast the 'char' to unsigned:
我有这个神器。当您使用 char 作为符号时,您没有问题。但是,当您将其用作整数(使用 isalpha() 等)并且 ASCII 代码大于 127 时,“char”将解释为“signed char”并且 isalpha() 返回异常。当我需要使用“char”作为整数时,我将“char”转换为无符号:
- isalpha((unsigned char)my_char);
- isalpha((unsigned char)my_char);
@n0rd: koi8 codepage uses ascii from 128 to 255 and other national codepages: http://www.asciitable.com/
@n0rd:koi8 代码页使用 128 到 255 的 ascii 和其他国家代码页:http: //www.asciitable.com/
回答by Arjun Gopkumar
In a character representation, you have 8 bits (1 byte) allotted.
Out of this, the first bit is used to represent sign. In the case of unsigned character, it uses all 8 bits to represent a number allowing 0 to 255 where
128-255 are called extended ASCII.
Due to the representation in the memory as I have described, we have -1 having the same value as 255, char(-2)==char(254)
在字符表示中,您分配了 8 位(1 个字节)。其中,第一位用于表示符号。在无符号字符的情况下,它使用所有 8 位来表示允许 0 到 255 的数字,其中 128-255 称为扩展 ASCII。由于我所描述的内存中的表示,我们有 -1 与 255 具有相同的值,char(-2)==char(254)