你能解释一下输出并指出错误吗

发布于 2024-10-21 23:09:50 字数 504 浏览 1 评论 0原文

char FramebufferUpdateRequest[11];
uint16_t val;
memset(FramebufferUpdateRequest, 0, 10);
FramebufferUpdateRequest[0] = 3;
FramebufferUpdateRequest[1] = 1;
val = 3;
memcpy(FramebufferUpdateRequest+6, &val, 2);
val = 2;
memcpy(FramebufferUpdateRequest+8, &val, 2);
FramebufferUpdateRequest[10]='\0';
printf("framerequest :: %c  %s\n", FramebufferUpdateRequest[1], FramebufferUpdateRequest);

这个 printf 的输出是空白,即“framerequest ::”。有人能指出我做错了什么吗?

在 gcc 4.1.2 中编译

char FramebufferUpdateRequest[11];
uint16_t val;
memset(FramebufferUpdateRequest, 0, 10);
FramebufferUpdateRequest[0] = 3;
FramebufferUpdateRequest[1] = 1;
val = 3;
memcpy(FramebufferUpdateRequest+6, &val, 2);
val = 2;
memcpy(FramebufferUpdateRequest+8, &val, 2);
FramebufferUpdateRequest[10]='\0';
printf("framerequest :: %c  %s\n", FramebufferUpdateRequest[1], FramebufferUpdateRequest);

output of this printf is Blank i.e "framerequest :: ".Can anyone point out what I am doing wrong?

compiled in gcc 4.1.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

反话 2024-10-28 23:09:50

我想你想写:

memset(FramebufferUpdateRequest, 0, 10);
FramebufferUpdateRequest[0] = '3'; //notice the difference
FramebufferUpdateRequest[1] = '1'; //notice the difference
val = '3';  //or var = ('3' << 1 | '3') if you want both bytes to have '3'
memcpy(FramebufferUpdateRequest+6, &val, 2);
val = '2';  //or var = ('2' << 1 | '2') if you want both bytes to have '2'

了解 '1'1 之间的区别:

   cout << (int) ('1') << endl;
   cout << (int) (1) << endl;

输出:( http://www.ideone.com/z3spn )

49
1

解释:'1' 是一个 字符文字,其 ascii 值为49,而1是一个整数。

I think you wanted to write:

memset(FramebufferUpdateRequest, 0, 10);
FramebufferUpdateRequest[0] = '3'; //notice the difference
FramebufferUpdateRequest[1] = '1'; //notice the difference
val = '3';  //or var = ('3' << 1 | '3') if you want both bytes to have '3'
memcpy(FramebufferUpdateRequest+6, &val, 2);
val = '2';  //or var = ('2' << 1 | '2') if you want both bytes to have '2'

Know the difference between '1' and 1:

   cout << (int) ('1') << endl;
   cout << (int) (1) << endl;

Output: ( http://www.ideone.com/z3spn )

49
1

Explanation: '1' is a character literal, whose ascii value is 49, whereas 1 is an integer.

懷念過去 2024-10-28 23:09:50

您正在将不可打印的字符分配给 FramebufferUpdateRequest

您需要以某种方式将其转换为整数(即​​使用循环和 %d)或可打印字符(例如,向每个元素添加 'A')。

维基百科显示了基本的可打印字符集。

You are assigning unprintable characters to FramebufferUpdateRequest.

You will need to somehow convert it either to integers (ie. use a loop and %d) or printable characters (for example, add 'A' to every element).

The basic set of printable characters is shown at Wikipedia.

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