首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

16进制转10进制有关问题

2013-06-25 
16进制转10进制问题#include stdio.h#include math.hint main (){int cc0xe555printf(c is %d,c)

16进制转10进制问题

#include <stdio.h>
#include <math.h>

int main ()
{
int c;
c=0xe555;
printf("c is %d",c); 
return 0;
}


输出的是58709怎么让系统识别这是有符号的16进制数,我要输出带符号的十进制 怎么弄?

谢谢。
[解决办法]

#include <stdio.h>
#include <math.h>

int main ()
{
int c;
c=0xe555;
printf("c is %x\n", c);//16进制
printf("c is %+d\n", c);//带符号十进制
return 0;
}

[解决办法]

/** 没有用到数学函数,不需要包含math.h */
#include <stdio.h>

/** main函数的标准写法只有int main(void)和int main(int argc, char* argv[])这两种 */ 
int main(void)
{
/** 在32位编译器中,16位有符号数用short int来声明 */
short int c;

c = 0xe555;
/** c的符号位为1,直接打印10进制为负数-6827 */
printf("c is %d\n", c); 
/** 对于16位的16进制而言,是不关注符号位的,所以应该先转为无符号数,然后用%04x限定只打印16位 */
printf("c is %04x\n", (unsigned short int)c); 

return 0;
}


引用:
#include <stdio.h>
#include <math.h>

int main ()
{
int c;
c=0xe555;
printf("c is %d",c); 
return 0;
}


输出的是58709怎么让系统识别这是有符号的16进制数,我要输出带符号的十进制 怎么弄?

谢谢。

热点排行