C/C++转换问题,无符号字符到字符
我有一个无符号字符,需要在 printf 之前将其转换为 char。所以示例代码如下所示:
unsigned char y = sample.result;
char x = (char)y;
printf("%c \n", x);
但是, printf
不会打印 x
但如果我使用 cout,x
会正确打印。我不知道为什么会这样。如何将 unsigned char 变量转换为 char?我做错了吗? reinterpret_casting 仅适用于指针,而我的不是指针。提前致谢。
编辑:命令提示符为我返回一个笑脸“☺”作为sample.result的值,它对应于unsigned char 1。显然netbeans无法打印这个笑脸。我不知道它是如何转化为笑脸的。有帮助吗?
编辑 2:我刚刚意识到你无法打印 char x = 1;在 netbeans 中,并在命令提示符中打印它会产生笑脸。原因? :(
i have an unsigned char which i need to convert to char before printf. So the example code goes like this:
unsigned char y = sample.result;
char x = (char)y;
printf("%c \n", x);
However, printf
does not print x
but if i use cout, x
prints correctly.I have no idea why so. How to i convert a unsigned char variable to a char? Am i doing it wrong? reinterpret_casting is only for pointer and mine are not pointers. Thanks in advance.
EDIT: Command prompt returns me a smiley face "☺" for the value of sample.result which corresponds to unsigned char 1. And apparently netbeans is unable to print this smiley face. I have no idea how it got translated into a smiley face. Any help?
EDIT 2: I just realized you can't print char x = 1; in netbeans, and printing it in command prompt yields the smiley face. Reasons? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果
char x = 1
并且您希望打印出 1,则需要在格式说明符中使用%d
而不是%c
。%c
打印数字的 ASCII 表示,对于 1 来说,它是一个不可打印的字符,称为“航向的开始。”If
char x = 1
and you want a 1 to be printed out, you need to use%d
instead of%c
in your format specifier.%c
prints the ASCII representation of the number, which for 1 is an unprintable character called "start of heading."尝试将
x
设置为int
而不是char
并查看是否有效。 C 规则要求所有 char 参数都转换为 int,而printf
是一个 C 函数。Try making
x
anint
instead ofchar
and see if that works. C rules required all char parameters to be converted to int, andprintf
is a C function.printf
的%c
转换仅适用于基本字符集中的字符。您似乎想要打印一个扩展字符,因此您必须使用适当的工具来打印。其类型为wchar_t
,打印格式为%lc
。但请注意,这很大程度上取决于您的执行环境、区域设置等。顺便说一句,在许多情况下,像
char
这样的窄类型只是转换为int
或unsigned
,所以不需要强制转换它,事实上除了你的标题表明你的问题与选角没有太大关系。The
%c
conversion forprintf
is only for characters in the basic character set. It seems you have an extended character that you want to print, so you'd have to use the appropriate tool for that. The type for that would bewchar_t
and the print format%lc
. But beware this depends a lot of your execution environment,locale
settings and stuff like that.And BTW in many circumstances narrow types like
char
are just converted toint
orunsigned
, so there is no need to cast it, and in fact other than your title suggests your problem has not much to do with casting.我对这个答案没有信心,但我只是编写了一个简单的测试程序,它编译并打印字母 A,而不将 unsigned char 转换回 char。
直接选角也有效:
按照你的方式做也有效......
所以我认为我现在做错了什么。或者也许是你的其他问题?
I'm not confident in this answer, but I just wrote a simple test program that compiles and prints the letter A without casting the unsigned char back to a char.
Casting directly also worked:
Doing it your way ALSO worked...
So I assume I'm doing something wrong, now. Or maybe something else is your problem?
转换为无符号
char uc
的方程这里是将
char c
算法。将无符号
uc
转换为char c
的算法。Here the equation of conversion
the algorithm for
char c
to unsignedchar uc
.the algorithm for unsigned
uc
tochar c
.