从无符号整型的转换Conversions from Unsigned Integral Types

通过截断高序位将无符号整数转换为较短的无符号或带符号整数,或者通过零扩展将其转换为一个较长的无符号或带符号整数(请参阅从无符号整型类型转换表)。An unsigned integer is converted to a shorter unsigned or signed integer by truncating the high-order bits, or to a longer unsigned or signed integer by zero-extending (see the Conversions from Unsigned Integral Types table).

当带整型的值降为较小的带符号整数,或者将无符号整数转换为其相应的带符号整数时,值保持不变(如果可以在新类型中表示该值)。When the value with integral type is demoted to a signed integer with smaller size, or an unsigned integer is converted to its corresponding signed integer, the value is unchanged if it can be represented in the new type. 但是,它表示的值将发生更改(如果设置符号位),如下面的示例所示。However, the value it represents changes if the sign bit is set, as in the following example.

int j;  
unsigned short k = 65533;  

j = k;  
printf_s( "%hd\n", j );   // Prints -3  

如果无法表示它,则结果是实现定义的。If it cannot be represented, the result is implementation-defined. 有关 Microsoft C 编译器如何处理整数降级的信息,请参阅类型强制转换See Type-Cast Conversions for information on the Microsoft C compiler's handling of demotion of integers. 整数转换或对整数进行类型强制会产生相同的行为。The same behavior results from integer conversion or from type casting the integer.

通过某种方式转换无符号值,以便保留其值并使其不能直接显示在 C 中。唯一的例外是从 unsigned long 到 float 的转换,这最多会丢失低序位。Unsigned values are converted in a way that preserves their value and is not representable directly in C. The only exception is a conversion from unsigned long to float, which loses at most the low-order bits. 否则,将保留值(带符号或无符号)。Otherwise, value is preserved, signed or unsigned. 将整型的值转换为浮点值且该值位于可表示的范围外时,结果是不确定的。When a value of integral type is converted to floating, and the value is outside the range representable, the result is undefined. (有关整型类型和浮点型的范围的信息,请参阅基本类型的存储。)(See Storage of Basic Types for information about the range for integral and floating-point types.)

下表汇总了来自无符号整型的转换。The following table summarizes conversions from unsigned integral types.

从无符号整型的转换Conversions from Unsigned Integral Types

FromFrom To 方法Method
unsigned char char 保留位模式;高序位将成为符号位Preserve bit pattern; high-order bit becomes sign bit
unsigned char shortshort 零扩展Zero-extend
unsigned char longlong 零扩展Zero-extend
unsigned char unsigned shortunsigned short 零扩展Zero-extend
unsigned char unsigned long 零扩展Zero-extend
unsigned char floatfloat 转换为 long;将 long 转换为 floatConvert to long; convert long to float
unsigned char 双精度double 转换为 long;将 long 转换为 doubleConvert to long; convert long to double
unsigned char long double 转换为 long;将 long 转换为 doubleConvert to long; convert long to double
unsigned shortunsigned short char 保留低位字节Preserve low-order byte
unsigned shortunsigned short shortshort 保留位模式;高序位将成为符号位Preserve bit pattern; high-order bit becomes sign bit
unsigned shortunsigned short longlong 零扩展Zero-extend
unsigned shortunsigned short unsigned char 保留低位字节Preserve low-order byte
unsigned shortunsigned short unsigned long 零扩展Zero-extend
unsigned shortunsigned short floatfloat 转换为 long;将 long 转换为 floatConvert to long; convert long to float
unsigned shortunsigned short 双精度double 转换为 long;将 long 转换为 doubleConvert to long; convert long to double
unsigned shortunsigned short long double 转换为 long;将 long 转换为 doubleConvert to long; convert long to double
unsigned long char 保留低位字节Preserve low-order byte
unsigned long shortshort 保留低位字Preserve low-order word
unsigned long longlong 保留位模式;高序位将成为符号位Preserve bit pattern; high-order bit becomes sign bit
unsigned long unsigned char 保留低位字节Preserve low-order byte
unsigned long unsigned shortunsigned short 保留低位字Preserve low-order word
unsigned long floatfloat 转换为 long;将 long 转换为 floatConvert to long; convert long to float
unsigned long 双精度double 直接转换为 doubleConvert directly to double
unsigned long long double 转换为 long;将 long 转换为 doubleConvert to long; convert long to double

Microsoft 专用Microsoft Specific

对于 Microsoft 32 位 C 编译器,unsigned int 类型与 unsigned long 类型等效。For the Microsoft 32-bit C compiler, the unsigned int type is equivalent to the unsigned long type. unsigned int 值的转换与 unsigned long 的转换的方式相同。Conversion of an unsigned int value proceeds in the same way as conversion of an unsigned long. 如果要转换的值大于正的最大带符号 long 值,则从 unsigned long 值到 float 的转换不准确。Conversions from unsigned long values to float are not accurate if the value being converted is larger than the maximum positive signed long value.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

赋值转换Assignment Conversions