Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ osx lion上的未签名字符限制为127?_C++_Unsigned Char - Fatal编程技术网

C++ osx lion上的未签名字符限制为127?

C++ osx lion上的未签名字符限制为127?,c++,unsigned-char,C++,Unsigned Char,我面临着一个奇怪的问题,正在开发我的MacOSX lion(在xcode4/clang下,尽管它可以用gcc4.2复制) 似乎,我不能为一个无符号char变量分配任何大于127的值。所以,当我分配 v = (unsigned char) 156; 或者,简单地说 std::cout << (unsigned char) 231 << std::endl; std::coutcout正在将数值转换为字符集中的字符(好吧,它试图…当你看不到任何东西时,它不是字符集的有效字

我面临着一个奇怪的问题,正在开发我的MacOSX lion(在xcode4/clang下,尽管它可以用gcc4.2复制)

似乎,我不能为一个无符号char变量分配任何大于127的值。所以,当我分配

v = (unsigned char) 156;
或者,简单地说

std::cout << (unsigned char) 231 << std::endl;

std::cout
cout
正在将数值转换为字符集中的字符(好吧,它试图…当你看不到任何东西时,它不是字符集的有效字符,从技术上讲,是终端决定了这一点)

将其强制转换为无符号整数


编辑添加:ildjarn在对您的问题的评论中提出了非常有效的观点;如果在调试器中运行此命令,您将看到该值确实是您期望的值

您希望看到什么符号来表示字符
(无符号字符)231
?在大多数系统中,这些字符都是扩展字符,需要特殊的终端设置才能显示为任何连贯的字符(即使可见)

试试这个:

unsigned char testChar = (unsigned char)231;
unsigned int testInt = (unsigned int)testChar;
std::cout << testInt << std::endl;
unsigned char testChar=(unsigned char)231;
unsigned int testInt=(unsigned int)testChar;

std::cout无符号字符的值不限于127位,但标准值仅为7位(因此为128位)。任何大于127的值都不代表任何字符(除非您使用某种类型的字符),因此不会打印任何字符。

您的理由很差;使用调试器进行调试,而不是控制台。尝试
v=(无符号字符)156;谢谢!我在gdb下运行它。键入casting to(
unsigned int
)确实有帮助!“改为将其转换为
int
。”不,改为转换为
未签名的
,否则可能会出现不必要的符号扩展。
unsigned char testChar = (unsigned char)231;
unsigned int testInt = (unsigned int)testChar;
std::cout << testInt << std::endl;