关于C#:char十六进制转义序列和十六进制数之间的差异

Difference between char hexadecimal escape sequence and hexadecimal number

为什么'x90'和0x90彼此不同。我知道一个是十六进制转义序列,另一个是十六进制数。但是,如果我将它们转换为十进制,则得到144,这应该是'x90'和0x90'的值。另外,书中说'x90'是负值,而0x90是正值。

据我所知,char只有1个字节,int是4,所以我们可以得到

1
2
char '\x90' = 1001 0000 ( 1 byte,8 bits)
int 0x90 = 1001 0000 0000 0000 0000 0000 0000 0000 (4 byte,32 bits)

但我还是不明白为什么char x90是负数,导致的值与int 0x90的值不同。

我的问题不是关于有符号和无符号字符,尽管这与我的问题有关,但我在问那些字符的into值。


Why are '\x90' and 0x90 different from each other(?)

第一个是转义序列,第二个是整数常量。它们具有相同的值和类型。

I fail to understand why the char x90 is negative and leads to difference value than int 0x90.

当分配给char时,它们都具有相同的值。

'\x90'0x90144都是c中的整数常数,3个都有相同的类型,int和相同的值:144。

一个char要么像一个signed char要么像一个unsigned char一样。显然,在OP的情况下,它的作用就像一个范围为[-128]的signed char。127。

char ch = 144;为例

分配144,这超出了OP的char的范围,导致实现定义的行为。这意味着实现可以做各种事情,比如像分配ch = 127;那样分配最大值。最常见的实现定义行为是重复地加/减256,直到和在范围内。这是144-256-->-112。

当把144看作8位unsigned char和-112看作8位有符号char时,它们都有相同的位模式1001 0000


charis 1字节=8位。如果我们考虑它"(only to be Unsigned"号当时正0x90 = 144),which is to hold没有问题。P></

charunsignedbut is not。meaning is reserved,一位正或负载(to the符号位)。therefore只有7位代表的正是used to the maximum number。27=128。当你试图assign 0x90is to char,它比空气日期2010年1月17 therefore正为最大值。This is signed Undefined溢出和行为。P></

我会在最implementations to the negatives wrap,知道恩- 128 -而不是becomes(128~144)=+16=-112-128。P></

比特may be the same the,but is not the解说。P></

disclaimer(the actual正为最大值:You can hold 7位是127,和我说让我说我最敏锐的感恩。that is one of the values 0 accounted must be for the real,知道公式2^n - 1 is where is the number of n位。the maximum value考虑1位;即使是1 2 1 = 2)P></


在C和0x90are both int'\x90'等文字,but they have a different value if the May and has is signed char型8位。在这房子的价值,'\x90'has is of -112whereas 0x90144Always。P></

标准:specifies this the cP></

6.4.4.4 Character constants.

§10 An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

因此,在'\x90'has the character of constant值(int)(char)0x90which is is if the char144型模式是默认signed 8位比前更广泛。-112otherwise is as its value to be the房子似乎你的在线系统。P></


代表的价值都一样。他们在哪里在差分is the used。P></

\x90is a character has char布尔型常数。inside of this is needed序列或者单或双quotes quotes.0x90is a constant of integer型inthexadecimal is not used within,和EN quotes.P></

As for正/负整数常数,除非他们有一个int型AISI型denoting后缀。自从0x90inside the range of an intFITS,它有积极的价值。如果你分配到charit to the value of type变量范围外的谎言,charis of the rian的定义和执行转换的仪表美。P></

similarly Escape,\x90has the unsigned char型序列。used within a character if such as constant '\x90'en is to the value转换char,不管一个人多是"the en is of char知道了范围的转换。P></

for example:P></

1
2
3
4
5
6
7
8
int a = 0x90;           // valid, has value 144
int b = '\x90';         // valid, has value -114
char c = 0x90;          // invalid, value out of range
char d = '\x90';        // invalid, same as above
unsigned char e[] ="\x90\x90";  // valid, string containing two bytes
char f[] ="\x90\x90";  // invalid, string containing two bytes but values are out of range
char g = \x90;          // invalid, compile error
char h ="0x90"         // valid, but contains the characters '0', 'x', '9', '0'


没有你在这里看到的是一个队列的可能性:P></

1
2
char c = '\x90' // 1001 0000 in binary
int i  = 0x90   // 1001 0000 in binary

如果你do something like thisP></

1
2
i = (int) c;    // i is ffffff90
                // casting is not necessary in C but this is just for this example

因为SIGN(最左位int和char)carries to the left over to填充空间。P></

编辑:我知道int char是8bit位宽的宽为32。我知道当你传递int int char茶最右位拷贝过我字符c is 1001 0000(0x90)当你拷贝它到过int值1111 1111、1111 is by会展1111 1111 1111 1111 1111 1111 1111 1111 1111 0000 1001(0xffffffffffffff90 because that is copied)1 to the left大胆thus让负面的价值。P></

intby the most char规则集与左前位是1负,在char c = 0x90thus C’是消极的P></