c: 将 char 值类型转换为 unsigned short

标签 c casting char short

以一段伪代码开始:

char a = 0x80;
unsigned short b;
b = (unsigned short)a;
printf ("0x%04x\r\n", b); // => 0xff80

根据我目前的理解,“char”根据定义既不是有符号字符也不是无符号字符,而是第三种类型的符号。

“a”首先从(可能与平台相关)8 位存储符号扩展到(可能又是平台特定的)16 位有符号短整数,然后转换为无符号短整数,这是怎么回事?

是否有一个c标准来决定展开的顺序?

该标准是否以任何方式指导如何处理“纯”字符的第三种类型的符号(我曾经称它为 X-char,x 表示未确定的符号)以便结果至少是确定性的?

PS:如果在赋值行的'a'前插入一个"(unsigned char)"语句,那么打印行的结果确实变成了0x0080。因此,只有连续两次类型转换才能提供某些意图的预期结果。

最佳答案

char 类型不是“第三”符号。它是 signed charunsigned char,具体是哪一个由实现定义。

这是由 C standard 的第 6.2.5p15 节规定的:

The three types char , signed char , and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

看起来在您的实现中,charsigned char 相同,因此因为该值为负数并且因为目标类型是无符号的,所以必须对其进行转换。

第 6.3.1.3 节规定了整数类型之间的转换是如何发生的:

1 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.

2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

3 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.

由于值 0x80 == -128 不能用 unsigned short 表示,因此发生第 2 段中的转换。

关于c: 将 char 值类型转换为 unsigned short,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282305/

相关文章:

c# - 如何转换为 C# 中的泛型参数?

c++ - 转换 “Stylee”

concat char* 与 char netbea

c++ - 无法创建光栅堆栈,因为我无法连接字符串(目录名 + 文件名)

从 fortran 调用 c 函数,argc 和 argv 有问题

c - C中的两种UDP套接字

c++ - 科学记数法对于 C 中的整数常量是否安全?

c++ - 避免在树结构中使用强制转换

c - AVR 模数转换 Atmega32

java - 在Java中将字符串中的多个字符转换为一个整数