📄 hex2ten.c
字号:
#include "string.h"
#include "stdio.h"
unsigned char g_BCD_array[40];
void HEX2DEC(unsigned char *hex_value,unsigned char *value_H)
{
unsigned char value;
value = *hex_value / 10;
*value_H = value;
*hex_value = *hex_value - value*10;
}
void DEC_MUL(unsigned char *BCD_array,unsigned char n,int array_count) //n<10
{
int i;
unsigned char temp_L=0,temp_H=0;
for(i=0;i<array_count;i++)
{
temp_L = BCD_array[i] * n;
temp_L += temp_H;
HEX2DEC(&temp_L,&temp_H);
BCD_array[i] = temp_L;
}
}
void DEC_MUL_ten(unsigned char *BCD_array,int array_count)
{
int i;
for(i = array_count-1;i>0;i--)
{
BCD_array[i] = BCD_array[i-1];
}
BCD_array[i] = 0;
}
void DEC_ADD(unsigned char *BCD_array1,unsigned char *BCD_array2,int array_count1,int array_count2)
{
int i;
unsigned char temp_L=0,temp_H=0;
for(i=0;i<array_count1;i++)
{
if(i>=array_count2)
temp_L = BCD_array1[i];
else
temp_L = BCD_array1[i] + BCD_array2[i];
temp_L += temp_H;
HEX2DEC(&temp_L,&temp_H);
BCD_array1[i] = temp_L;
}
}
void DEC_MUL_16(unsigned char *T_BCD_array,int array_count)
{
unsigned char BCD_array[40];
memcpy(BCD_array,T_BCD_array,array_count);
DEC_MUL_ten(T_BCD_array,array_count);
DEC_MUL(BCD_array,6,array_count);
DEC_ADD(T_BCD_array,BCD_array,array_count,array_count);
}
void hex2ten(unsigned short *T_value,int length)
{
unsigned short value[20];
unsigned char numBCD[3]={0,0,0};
unsigned char temp_L=0,temp_H=0;
int i;
memset(g_BCD_array,0,40);
memset(value,0x0000,40);
memcpy(value,T_value,length);
for(i = 19;i>=0;i--)
{
DEC_MUL_16(g_BCD_array,40);
temp_L = (unsigned char)(value[i] >>12);
temp_L &= 0x0f;
HEX2DEC(&temp_L,&temp_H);
numBCD[0] = temp_L;
numBCD[1] = temp_H;
DEC_ADD(g_BCD_array,numBCD,40,3);
DEC_MUL_16(g_BCD_array,40);
temp_L = (unsigned char)(value[i] >>8);
temp_L &= 0x0f;
HEX2DEC(&temp_L,&temp_H);
numBCD[0] = temp_L;
numBCD[1] = temp_H;
DEC_ADD(g_BCD_array,numBCD,40,3);
DEC_MUL_16(g_BCD_array,40);
temp_L = (unsigned char)((value[i] >>4)&0x000f);
temp_L &= 0x0f;
HEX2DEC(&temp_L,&temp_H);
numBCD[0] = temp_L;
numBCD[1] = temp_H;
DEC_ADD(g_BCD_array,numBCD,40,3);
DEC_MUL_16(g_BCD_array,40);
temp_L = (unsigned char)(value[i] &0x000f);
temp_L &= 0x0f;
HEX2DEC(&temp_L,&temp_H);
numBCD[0] = temp_L;
numBCD[1] = temp_H;
DEC_ADD(g_BCD_array,numBCD,40,3);
}
printf("\n\r");
for(i = 39;i>=0;i--)
{
printf("%d ",g_BCD_array[i]);
}
printf("\n\r");
}
main()
{
unsigned short value[8]={1,2,3,4,0,0,0,0};
hex2ten(value,8);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -