[英]Can you please Explain the output and point out mistake
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 ie "framerequest :: ".Can anyone point out what I am doing wrong? 此printf的输出为Blank,即“ framerequest ::”。有人能指出我做错了吗?
compiled in gcc 4.1.2 在gcc 4.1.2中编译
You are assigning unprintable characters to FramebufferUpdateRequest
. 您正在将不可打印的字符分配给
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). 您将需要以某种方式将其转换为整数(即,使用循环和
%d
)或可打印的字符(例如,将'A'
添加到每个元素)。
The basic set of printable characters is shown at Wikipedia . 可打印字符的基本设置在Wikipedia中显示 。
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
: 知道
'1'
和1
之间的区别:
cout << (int) ('1') << endl;
cout << (int) (1) << endl;
Output: ( http://www.ideone.com/z3spn ) 输出:( http://www.ideone.com/z3spn )
49
1
Explanation: '1'
is a character literal , whose ascii value is 49
, whereas 1
is an integer. 说明:
'1'
是字符文字 ,其ascii值为49
,而1
是整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.