简体   繁体   English

将unsigned char转换为signed int

[英]Converting unsigned char to signed int

Consider the following program 考虑以下程序

void main(){
  char t = 179;
  printf("%d ",t);
}

Output is -77. 输出为-77。 But binary representation of 179 is 但是179的二进制表示是

10110011

So, shouldn't output be -51, considering 1st bit is singed bit. 因此,考虑到第一位是单位,则不应输出为-51。 Binary representation of -77 is -77的二进制表示为

11001101

It seems bit order is reversed. 似乎位顺序是相反的。 What's going on? 这是怎么回事? Please somebody advice. 请有人指教。

You claimed that the binary representation of -77 is 11001101 , but the reversal is yours. 您声称-77的二进制表示形式是11001101 ,但是您的冲销是您的。 The binary representation of -77 is 10110011 . -77的二进制表示为10110011

Binary 10110011 unsigned is decimal 179 . 无符号的二进制10110011是十进制179

Binary 10110011 signed is decimal -77 . 签名的二进制10110011是十进制-77

You assigned the out-of-range value 179 to a signed char . 您将超出范围的值179分配给一个已signed char It might theoretically be Undefined Behaviour, but apart from throwing an error, it would be a very poor compiler that placed anything but that 8-bit value in the signed char . 从理论上讲,它可能是未定义的行为,但是除了引发错误之外,它是一个非常糟糕的编译器,只能将8位值之外的任何内容都放置在带signed char

But when printed, it's interpreted as a negative number because b7 is set. 但是在打印时,由于设置了b7,它被解释为负数。

Looks like char is a signed type on your system. 看起来char是系统上的带符号类型。 The valid range for a char would be [-128, 127] char的有效范围为[-128,127]

By using 通过使用

char t = 179;

the compiler uses the 2's complement of 179 (which is most likely -77) and assigns that value to t . 编译器使用179的2的补码(最有可能是-77),并将该值分配给t

To convert between a positive and a negative number in 2's complement you invert all the bits and then you add 1. 要在2的补码中的正数和负数之间转换,请反转所有位,然后加1。

10110011 10110011

01001100 (invert all bits) 01001100(将所有位反转)

01001101 (add one) 01001101(加一)

That is 77 decimal 那是77位十进制

Char could be signed or unsigned: that's up to your compiler. Char可以是签名的也可以是未签名的:这取决于您的编译器。

If signed, it could be 2s or 1 complement. 如果签名,则可以是2s或1个补码。

Furthermore, it could be larger than 8 bits, although sizeof char is defined to be 1. 此外,尽管sizeof char被定义为1,但它可能大于8位。

So it's inadvisable to rely on a specific representation. 因此,不建议依赖于特定的表示形式。

Please note, that on many systems char is signed. 请注意,在许多系统上, char是签名的。 Hence, when you assign 179 (which is of type int ) to a char this value is outside of char range, hence it is unspecified behaviour. 因此,当您将179(类型为int )分配给char此值不在char范围内,因此未指定行为。

6.3.1.3 Signed and unsigned integers: 6.3.1.3有符号和无符号整数:

When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. 将具有整数类型的值转换为_Bool以外的其他整数类型时,如果该值可以用新类型表示,则该值不变。 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more unsigned integer than the maximum value that can be represented in the new type until the value is in the range of the new conversion to type. 否则,如果新类型是无符号的,则通过重复添加或减去比新类型可表示的最大值多一个无符号整数来转换值,直到该值在新的转换类型范围内。 Otherwise, the new type is signed and the value cannot be represented in it; 否则,将对新类型进行签名,并且无法在其中表示值; either the result is implementation-defined or an implementation-defined signal is raised. 结果是实现定义的,还是引发实现定义的信号。

If you change the type to unsigned char your program will perform correctly. 如果将类型更改为unsigned char程序将正确执行。

Also note that char , signed char and unsigned char are 3 distinct types, unlike int and signed int . 还要注意,与intsigned int不同, charsigned charunsigned char是3种不同的类型。 The signedness of char is implementation defined. char的签名由实现定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM