#include<stdio.h>
#include"utils.h"
#include<string.h>
int Hex2Char(int fromi,char *toc)
{
if(fromi>=0&&fromi<=9)
{
*toc= fromi+'0';
}
else if(fromi>=10&&fromi<=15)
{
*toc = fromi+'A'-10;
}
else
{
printf("不在范围内!");
}
return 0;
}
int Char2Hex(char fromc,int *toi)
{
if(fromc>='0'&& fromc<='9')
{
*toi= fromc-'0';
}
else if(fromc>='A'&& fromc<='F')
{
*toi= fromc-'A'+10;
}
else{
printf("不在范围内");
}
return 0;
}
main.c
#include<stdio.h>
#include"utils.h"
int main(){
unsigned char * c;
int n=0;
int i=0;
for(i=0;i<=15;i++)//测试数字0-15转为字符
{
Hex2Char(n, &c);
printf("c=%c ",c);
n=n+1;
}
char m='A';
int y='0';
int k;
for(i=0;i<=9;i++)//测试字符0-9转为数字
{
Char2Hex(y, &k);
printf("k=%d ",k);
y=y+1;
}
for(i=0;i<=5;i++)//测试字符A-F转为数字
{
Char2Hex(m, &k);
printf("k=%d ",k);
m=m+1;
}
return 0;
}