#include <stdio.h>
#include <stdlib.h>
typedef unsigned char BOOL_T;
typedef unsigned char U8_T;
typedef signed char S8_T;
typedef unsigned short U16_T;
typedef signed short S16_T;
typedef unsigned int U32_T;
typedef signed int S32_T;
typedef unsigned __int64 U64_T;
typedef signed __int64 S64_T;
typedef float F32_T;
typedef double F64_T;
typedef char * STR_T;
U8_T x_mod(U16_T num)//球余数
{
U8_T result = 0;
result = num - (num/16)*16;
return result;
}
/*************
4625/16=289 余数1 商289
289/16=18 余数1
18/16=1 余数2
1/16=0 余数1
所以4625d=1211h
******************/
U16_T DecToHex(U16_T num)
{
U16_T result = 0;
U16_T tmp_result = 0;
if (num < 255)
{
result = ((num/16)<<4) + ((num%16)&0xF);
}
else
{
result |= x_mod(num); //余数
tmp_result = (num - x_mod(num))/16;//商
result |= x_mod(tmp_result)<<4; //余数
tmp_result = (tmp_result - x_mod(tmp_result))/16 ;//商
result |= x_mod(tmp_result)<<8; //余数
tmp_result = (tmp_result - x_mod(tmp_result))/16;//商
result |= (x_mod(tmp_result)<<12);//余数
}
return result;
}
int main()
{
printf("TEST:%5x\n", DecToHex(10400));
printf("TEST:%5x\n", DecToHex(19));
printf("TEST:%5x\n", DecToHex(15));
printf("TEST:%5x\n", DecToHex(30825));
return 0;
}
运行结果显示: