C/C++转换问题,无符号字符到字符

发布于 2024-10-19 07:02:06 字数 551 浏览 2 评论 0原文

我有一个无符号字符,需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

他是夢罘是命 2024-10-26 07:02:06

如果 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."

司马昭之心 2024-10-26 07:02:06

尝试将 x 设置为 int 而不是 char 并查看是否有效。 C 规则要求所有 char 参数都转换为 int,而 printf 是一个 C 函数。

Try making x an int instead of char and see if that works. C rules required all char parameters to be converted to int, and printf is a C function.

夜夜流光相皎洁 2024-10-26 07:02:06

printf%c 转换仅适用于基本字符集中的字符。您似乎想要打印一个扩展字符,因此您必须使用适当的工具来打印。其类型为 wchar_t ,打印格式为 %lc。但请注意,这很大程度上取决于您的执行环境、区域设置等。

顺便说一句,在许多情况下,像 char 这样的窄类型只是转换为 intunsigned,所以不需要强制转换它,事实上除了你的标题表明你的问题与选角没有太大关系。

The %c conversion for printf 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 be wchar_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 to int or unsigned, 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.

故人如初 2024-10-26 07:02:06

我对这个答案没有信心,但我只是编写了一个简单的测试程序,它编译并打印字母 A,而不将 unsigned char 转换回 char。

#include <stdio.h>
int main()
{
  unsigned char c = 'A';
  printf("%c\n",c);
  return 0;
}

直接选角也有效:

#include <stdio.h>
int main()
{
  unsigned char c = 'A';
  printf("%c\n",(char)c);
  return 0;
}

按照你的方式做也有效......

#include <stdio.h>
int main()
{
  unsigned char c = 'A';
  char d = (char)c;
  printf("%c\n",d);
  return 0;
}

所以我认为我现在做错了什么。或者也许是你的其他问题?

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.

#include <stdio.h>
int main()
{
  unsigned char c = 'A';
  printf("%c\n",c);
  return 0;
}

Casting directly also worked:

#include <stdio.h>
int main()
{
  unsigned char c = 'A';
  printf("%c\n",(char)c);
  return 0;
}

Doing it your way ALSO worked...

#include <stdio.h>
int main()
{
  unsigned char c = 'A';
  char d = (char)c;
  printf("%c\n",d);
  return 0;
}

So I assume I'm doing something wrong, now. Or maybe something else is your problem?

苏大泽ㄣ 2024-10-26 07:02:06

转换为无符号 char uc 的方程

这里是将char c 算法

return (c>127) ? (unsigned char)(-(256 + c)) : (unsigned char)c;

。将无符号uc 转换为char c 的算法。

return (uc < 0) ? (char)(256-(char)abs(uc)) : (char)uc;

Here the equation of conversion

the algorithm for char c to unsigned char uc.

return (c>127) ? (unsigned char)(-(256 + c)) : (unsigned char)c;

the algorithm for unsigned uc to char c.

return (uc < 0) ? (char)(256-(char)abs(uc)) : (char)uc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文