C语言 如何在 C 中将无符号 int 转换或转换为 int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5129498/
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
How to cast or convert an unsigned int to int in C?
提问by Eric Brotto
My apologies if the question seems weird. I'm debugging my code and this seems to be the problem, but I'm not sure.
如果问题看起来很奇怪,我深表歉意。我正在调试我的代码,这似乎是问题所在,但我不确定。
Thanks!
谢谢!
回答by Chris Cooper
It depends on what you want the behaviour to be. An intcannot hold many of the values that an unsigned intcan.
这取决于您希望行为是什么。一个int不能拥有一个可以拥有的许多值unsigned int。
You can cast as usual:
你可以像往常一样投射:
int signedInt = (int) myUnsigned;
but this will cause problems if the unsignedvalue is past the max intcan hold. This means half of the possible unsignedvalues will result in erroneous behaviour unless you specifically watch out for it.
但是如果该unsigned值超过了最大int可以容纳的值,这将导致问题。这意味着一半的可能unsigned值将导致错误行为,除非您特别注意。
You should probably reexamine how you store values in the first place if you're having to convert for no good reason.
如果您无缘无故地转换,您可能应该首先重新检查如何存储值。
EDIT:As mentioned by ProdigySim in the comments, the maximum value is platform dependent. But you can access it with INT_MAXand UINT_MAX.
编辑:正如 ProdigySim 在评论中提到的,最大值取决于平台。但是您可以使用INT_MAX和访问它UINT_MAX。
For the usual 4-byte types:
对于通常的 4 字节类型:
4 bytes = (4*8) bits = 32 bits
If all 32 bits are used, as in unsigned, the maximum value will be 2^32 - 1, or 4,294,967,295.
如果使用所有 32 位,如unsigned,最大值将为 2^32 - 1,或4,294,967,295。
A signed inteffectively sacrifices one bit for the sign, so the maximum value will be 2^31 - 1, or 2,147,483,647. Note that this is half of the other value.
有符号int有效地为符号牺牲了一位,因此最大值将为 2^31 - 1,或2,147,483,647。请注意,这是其他值的一半。
回答by sgokhales
Unsigned int can be converted to signed (or vice-versa) by simple expression as shown below :
无符号整数可以通过简单的表达式转换为有符号(或反之亦然),如下所示:
unsigned int z;
int y=5;
z= (unsigned int)y;
Though not targeted to the question, you would like to read following links :
虽然不是针对这个问题,但您想阅读以下链接:
回答by Jeremiah Willcock
If you have a variable unsigned int x;, you can convert it to an intusing (int)x.
如果您有一个变量unsigned int x;,您可以将其转换为intusing (int)x。
回答by DarkDust
It's as simple as this:
就这么简单:
unsigned int foo;
int bar = 10;
foo = (unsigned int)bar;
Or vice versa...
或相反亦然...
回答by Lundin
If an unsigned int and a (signed) int are used in the same expression, the signed int gets implicitly converted to unsigned. This is a rather dangerous feature of the C language, and one you therefore need to be aware of. It may or may not be the cause of your bug. If you want a more detailed answer, you'll have to post some code.
如果 unsigned int 和 (signed) int 在同一个表达式中使用,signed int 将隐式转换为 unsigned。这是 C 语言的一个相当危险的特性,因此您需要注意这一特性。它可能是也可能不是您的错误的原因。如果您想要更详细的答案,则必须发布一些代码。
回答by Jixuan Cheng
Some explain from C++Primer 5thPage 35
一些解释来自C++Primer 5thPage 35
If we assign an out-of-range value to an object of unsigned type, the result is the remainder of the value modulo the number of values the target type can hold.
如果我们将一个超出范围的值分配给一个无符号类型的对象,结果是该值的余数以目标类型可以容纳的值数为模。
For example, an 8-bit unsigned char can hold values from 0 through 255, inclusive. If we assign a value outside the range, the compiler assigns the remainder of that value modulo 256.
例如,一个 8 位无符号字符可以保存从 0 到 255(包括 0 到 255)的值。如果我们分配一个超出范围的值,编译器将分配该值的余数,取模 256。
unsigned char c = -1; // assuming 8-bit chars, c has value 255
If we assign an out-of-range value to an object of signed type, the result is undefined. The program might appear to work, it might crash, or it might produce garbage values.
如果我们将一个超出范围的值分配给一个有符号类型的对象,结果是未定义的。程序可能看起来工作正常,可能会崩溃,或者可能会产生垃圾值。
Page 160: If any operand is an unsigned type, the type to which the operands are converted depends on the relative sizes of the integral types on the machine.
第 160 页:如果任何操作数是无符号类型,则操作数转换为的类型取决于机器上整数类型的相对大小。
... When the signedness differs and the type of the unsigned operand is the same as or larger than that of the signed operand, the signed operand is converted to unsigned.
... 当有符号数不同且无符号操作数的类型等于或大于有符号操作数时,将有符号操作数转换为无符号操作数。
The remaining case is when the signed operand has a larger type than the unsigned operand. In this case, the result is machine dependent. If all values in the unsigned type fit in the large type, then the unsigned operand is converted to the signed type. If the values don't fit, then the signed operand is converted to the unsigned type.
剩下的情况是有符号操作数的类型大于无符号操作数。在这种情况下,结果取决于机器。如果无符号类型中的所有值都适合大类型,则无符号操作数将转换为有符号类型。如果值不合适,则有符号操作数将转换为无符号类型。
For example, if the operands are long and unsigned int, and int and long have the same size, the length will be converted to unsigned int. If the long type has more bits, then the unsigned int will be converted to long.
例如,如果操作数是 long 和 unsigned int,并且 int 和 long 的大小相同,则长度将转换为 unsigned int。如果 long 类型有更多位,那么 unsigned int 将被转换为 long。
I found reading this book is very helpful.
我发现阅读这本书很有帮助。

