prog我为一些整数值编写了作品,但并非所有...为什么?
程序输出
int value is abcdef78
first byte is 78 addr is 2686741
second byte is ef addr is 2686742
third byte is cd addr is 2686743
fourth byte is ab addr is 2686744
Process returned 0 (0x0) execution time : 0.015 s
Press any key to continue.
预期产出
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0xabcdef78;
int *ip;
printf("int value is %x\n",i); // *ip contents of address
char c;
char *cp;
cp=&i;
c = *cp;
printf("first byte is %x addr is %d\n",*cp++,cp);
printf("second byte is %x addr is %d\n",*cp++,cp);
printf("third byte is %x addr is %d\n",*cp++,cp);
printf("fourth byte is %x addr is %d\n",*cp++,cp);
return 0;
}
代码:
<div id="container">
<div id="tempMain">
<div id="textTemp" class="temp">
<div>Text</div>
</div>
</div>
<div id="temEditor"><div id="tempEditorView"></div></div></div>
$(function(){$("#textTemp").draggable({
helper: "clone",
zIndex: 2500,
});
$( "#tempEditorView" ).droppable({
accept: '#textTemp',
drop: function( event, ui ) {
var html = '<div id="test" style="background: #eee; width: 80%;
margin: 10px auto; padding: 10px;"><p contenteditable="true"
style="padding: 5px;">Add your text here.</p><div
style="padding:20px;" class="tempDraggableArea"></div></div>';
$(html).appendTo(this).hide().slideDown();
}
});
$('#tempEditorView').sortable({
handle: '.tempDraggableArea'
});
});
答案 0 :(得分:0)
原因是你的char被提升为符号扩展名为int。二进制形式的0xef是:
1110 1111
并将其提升为带有符号扩展名的32位整数:
1111 1111 1111 1111 1111 1111 1110 1111
二进制形式的0x78是:
0111 1000
最重要的位是0,所以它会像这样得到提升:
0000 0000 0000 0000 0000 0000 0111 1000
有两种解决方案:
不要将您的char打印为32位值,即使用%hhx而不是%x。
使你的字符无符号,即unsigned char * cp而不是char * cp
答案 1 :(得分:0)
你应该像
一样修改你的代码<li id="c2b79d1e-e14f-43cb-9a58-2c57231ef3a8" class="k-item k-state-focused k-state-selected" data-offset-index="0" unselectable="on" role="option" tabindex="-1" aria-selected="true">Is equal to</li>
<li class="k-item" data-offset-index="1" unselectable="on" role="option" tabindex="-1">Is not equal to</li>
<li class="k-item" data-offset-index="2" unselectable="on" role="option" tabindex="-1">Starts with</li>
<li class="k-item" data-offset-index="3" unselectable="on" role="option" tabindex="-1">Contains</li>
如你所见:
#include <stdio.h>
int main(void)
{
int i = 0xabcdef78;
printf("int value is %x\n", i); // *ip contents of address
unsigned char *cp;
cp = (unsigned char *)(&i);
printf("1st byte is 0x%02X addr is %p\n", cp[0], (void *)(&cp[0]) );
printf("2nd byte is 0x%02X addr is %p\n", cp[1], (void *)(&cp[1]) );
printf("3rd byte is 0x%02X addr is %p\n", cp[2], (void *)(&cp[2]) );
printf("4th byte is 0x%02X addr is %p\n", cp[3], (void *)(&cp[3]) );
return 0;
}
并且传递arg必须为%p
void *
之后加入它。printf
指向整数的字节,否则,正如您所面临的那样,大于unsigned char
的值将被视为负数。输出
7F
答案 2 :(得分:-1)
删除声明
int *ip;
因为你从不使用它(不重要)。然后:
而不是
printf("first byte is %x addr is %d\n",*cp++,cp);
printf("second byte is %x addr is %d\n",*cp++,cp);
printf("third byte is %x addr is %d\n",*cp++,cp);
printf("fourth byte is %x addr is %d\n",*cp++,cp);
使用
printf("first byte is %hhx, addr is %ld\n", *cp++, cp);
printf("second byte is %hhx, addr is %ld\n", *cp++, cp);
printf("third byte is %hhx, addr is %ld\n", *cp++, cp);
printf("fourth byte is %hhx, addr is %ld\n", *cp++, cp);
因为程序的逻辑是正确的,但输出格式错误。