Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103133
  • 博文数量: 51
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-08 10:52
文章分类

全部博文(51)

文章存档

2016年(5)

2015年(3)

2014年(43)

我的朋友

分类: C/C++

2014-03-15 09:26:32

原文地址:Linux下的进制转换函数 作者:乐百事2

/*
  * 17-May-2012 Auther.Zhang 0.0 Version
  * Copyright & the right of find interpretation auther
  * synopsis:特殊的进制转换
  *
  */
#include
#include

typedef unsigned char Uchar; //1Byte
typedef unsigned short int Uint; //2Byte
typedef unsigned long Ulong; //4Byte


/****************************************************************
函数说明 :十进制转十六进制,字符转换不涉及数值大小,特殊用途
入口参数 :需要转换的十进制数
返回类型 :转换完成的十六进制数
初始时间 :2012-5-17 Lebaishi
修改记录 :
用途功能 :将十进制直接转换为十六进制,例如12直接转为0x12等
****************************************************************/
char Dec2hex(int dec_data) 
Uchar *s=malloc(10);
Ulong bcd_data;
sprintf(s,"%d",dec_data); //dec_data=12,s="12"
sscanf(s,"%x",&bcd_data);
free(s);
    return bcd_data; 


/****************************************************************
函数说明 :十六进制转十进制,字符转换不涉及数值大小,特殊用途
入口参数 :需要转换的十六进制数
返回类型 :转换完成的十进制数
初始时间 :2012-5-17 Lebaishi
修改记录 :
用途功能 :将十六进制直接转换为十进制,例如0x12直接转为12等
****************************************************************/
char hex2Dec(Uchar bcd_data) 
Uchar *s=malloc(10);
Ulong dec_data;
sprintf(s,"%x",bcd_data); 
sscanf(s,"%d",&dec_data);
free(s);
  return dec_data; 


/*****************  主函数 ******************/
int main()
{
char hex_dec=0;
char dec_hex=0;
hex_dec = Dec2hex(12);
printf("Dec2hex(12):\n");
printf("come in:12 Dec  output:%d\n",hex_dec);
printf("come in:12 Dec  output:%#x\n",hex_dec);
dec_hex = hex2Dec(0x12);
printf("hex2Dec(0x12):\n");
printf("come in:0x12    output:%d\n",dec_hex);
printf("come in:0x12    output:%#x\n,dec_hex");
return 1;
}
阅读(721) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~