加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C语言十进制数转化为十六进制数

(2012-05-19 15:59:23)
标签:

杂谈

分类: C程序设计
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reversestr(char*source,char target[],unsigned int length);
void tohex(unsigned long num,char*hexStr);
int main()
{
char hexStr[16];
int num;
printf("请输入一个十进制数:\n");
scanf("%d",&num);
tohex(num,hexStr);
printf("转换成的十六进制为:%s\n",hexStr);
system("pause");
}
void reversestr(char*source,char target[],unsigned int length)
{
unsigned int i;
for(i=0;i<length;i++)
target[i]=source[length-1-i];
target[i]=0;
}
void tohex(unsigned long num,char*hexStr)
{
unsigned long n=num;
char hextable[]="0123456789ABCDEF";
char temphex[16],hex[16];
unsigned long int i=0;
while(n)
{
temphex[i++]=hextable[n];
n/=16;
}
temphex[i]=0;
reversestr(temphex,hex,i);
strcpy(hexStr,hex);
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有