在C/ c++中,unsigned char是用来干什么的?它和常规char有什么不同?
当前回答
例如unsigned char的用法:
Unsigned char经常用于计算机图形,它经常(虽然不总是)为每个颜色组件分配一个字节。通常可以看到RGB(或RGBA)颜色表示为24(或32)位,每个位都是unsigned char。由于unsigned char值落在[0,255]范围内,这些值通常被解释为:
0表示完全缺乏给定的颜色组件。 255表示某一特定色素的100%。
所以你最终会得到RGB红色为(255,0,0)->(100%红,0%绿,0%蓝)。
Why not use a signed char? Arithmetic and bit shifting becomes problematic. As explained already, a signed char's range is essentially shifted by -128. A very simple and naive (mostly unused) method for converting RGB to grayscale is to average all three colour components, but this runs into problems when the values of the colour components are negative. Red (255, 0, 0) averages to (85, 85, 85) when using unsigned char arithmetic. However, if the values were signed chars (127,-128,-128), we would end up with (-99, -99, -99), which would be (29, 29, 29) in our unsigned char space, which is incorrect.
其他回答
一些人在谷歌上找到了这个,人们对此进行了讨论。
无符号字符基本上是一个单字节。所以,如果你需要一个字节的数据,你可以使用它(例如,也许你想用它来设置标志的开启和关闭,以传递给一个函数,就像在Windows API中经常做的那样)。
例如unsigned char的用法:
Unsigned char经常用于计算机图形,它经常(虽然不总是)为每个颜色组件分配一个字节。通常可以看到RGB(或RGBA)颜色表示为24(或32)位,每个位都是unsigned char。由于unsigned char值落在[0,255]范围内,这些值通常被解释为:
0表示完全缺乏给定的颜色组件。 255表示某一特定色素的100%。
所以你最终会得到RGB红色为(255,0,0)->(100%红,0%绿,0%蓝)。
Why not use a signed char? Arithmetic and bit shifting becomes problematic. As explained already, a signed char's range is essentially shifted by -128. A very simple and naive (mostly unused) method for converting RGB to grayscale is to average all three colour components, but this runs into problems when the values of the colour components are negative. Red (255, 0, 0) averages to (85, 85, 85) when using unsigned char arithmetic. However, if the values were signed chars (127,-128,-128), we would end up with (-99, -99, -99), which would be (29, 29, 29) in our unsigned char space, which is incorrect.
摘自《c语言编程》一书:
有符号或无符号限定词可以应用于char或任何整数。无符号数 都是正的或零的,并且服从对2^n取模的算术法则,其中n是数字 类型中的位。例如,如果字符是8位,unsigned char变量就有值 在0到255之间,而有符号字符的值在-128到127之间 补机)。纯字符是有符号字符还是无符号字符取决于机器, 但是可打印的字符总是正的。
在c++中,有三种不同的字符类型:
字符 签署了字符 无符号字符
如果你在文本中使用字符类型,请使用非限定字符:
它是像'a'或'0'这样的字符字面量的类型(仅在c++中,在C中它们的类型是int) 它是构成C字符串的类型,如"abcde"
它也是一个数字值,但是没有指定该值是有符号还是无符号。小心通过不平等进行字符比较——尽管如果你将自己限制在ASCII(0-127),你是安全的。
如果你使用字符类型作为数字,请使用:
Signed char,它至少提供了-127到127的范围。(-128到127是常见的) Unsigned char,它至少提供了0到255的范围。
“至少”,因为c++标准只给出了每个数字类型需要涵盖的最小值范围。Sizeof (char)被要求为1(即一个字节),但一个字节在理论上可以是32位。Sizeof仍然会报告它的大小为1 -这意味着你可以有Sizeof (char) == Sizeof (long) == 1。
有符号char和无符号char都表示1字节,但它们的范围不同。
Type | range
-------------------------------
signed char | -128 to +127
unsigned char | 0 to 255
在signed char中,如果我们考虑char letter = 'A', 'A'在ASCII/Unicode中代表65的二进制,如果65可以存储,-65也可以存储。在ASCII/Unicode中没有负二进制值,所以不需要担心负数。
例子
#include <stdio.h>
int main()
{
signed char char1 = 255;
signed char char2 = -128;
unsigned char char3 = 255;
unsigned char char4 = -128;
printf("Signed char(255) : %d\n",char1);
printf("Unsigned char(255) : %d\n",char3);
printf("\nSigned char(-128) : %d\n",char2);
printf("Unsigned char(-128) : %d\n",char4);
return 0;
}
输出-:
Signed char(255) : -1
Unsigned char(255) : 255
Signed char(-128) : -128
Unsigned char(-128) : 128