24位BIN轉BCD碼:
//--------------start of file---------------- -
-
extern void bin2bcd(unsigned char *output, unsigned long input); -
-
void bin2bcd(unsigned char *output, unsigned long input) -
{ -
*output = 0; -
if (input >= 4000000000) { *output += 0x40; input -= 4000000000; } -
if (input >= 2000000000) { *output += 0x20; input -= 2000000000; } -
if (input >= 1000000000) { *output += 0x10; input -= 1000000000; } -
if (input >= 800000000) { *output += 0x08; input -= 800000000; } -
if (input >= 400000000) { *output += 0x04; input -= 400000000; } -
if (input >= 200000000) { *output += 0x02; input -= 200000000; } -
if (input >= 100000000) { *output += 0x01; input -= 100000000; } -
output++; -
-
*output = 0; -
if (input >= 80000000) { *output += 0x80; input -= 80000000; } -
if (input >= 40000000) { *output += 0x40; input -= 40000000; } -
if (input >= 20000000) { *output += 0x20; input -= 20000000; } -
if (input >= 10000000) { *output += 0x10; input -= 10000000; } -
if (input >= 8000000) { *output += 0x08; input -= 8000000; } -
if (input >= 4000000) { *output += 0x04; input -= 4000000; } -
if (input >= 2000000) { *output += 0x02; input -= 2000000; } -
if (input >= 1000000) { *output += 0x01; input -= 1000000; } -
output++; -
-
*output = 0; -
if (input >= 800000) { *output += 0x80; input -= 800000; } -
if (input >= 400000) { *output += 0x40; input -= 400000; } -
if (input >= 200000) { *output += 0x20; input -= 200000; } -
if (input >= 100000) { *output += 0x10; input -= 100000; } -
if (input >= 80000) { *output += 0x08; input -= 80000; } -
if (input >= 40000) { *output += 0x04; input -= 40000; } -
if (input >= 20000) { *output += 0x02; input -= 20000; } -
if (input >= 10000) { *output += 0x01; input -= 10000; } -
output++; -
-
*output = 0; -
if (input >= 8000) { *output += 0x80; input -= 8000; } -
if (input >= 4000) { *output += 0x40; input -= 4000; } -
if (input >= 2000) { *output += 0x20; input -= 2000; } -
if (input >= 1000) { *output += 0x10; input -= 1000; } -
if (input >= 800) { *output += 0x08; input -= 800; } -
if (input >= 400) { *output += 0x04; input -= 400; } -
if (input >= 200) { *output += 0x02; input -= 200; } -
if (input >= 100) { *output += 0x01; input -= 100; } -
output++; -
-
*output = 0; -
if (input >= 80) { *output += 0x80; input -= 80; } -
if (input >= 40) { *output += 0x40; input -= 40; } -
if (input >= 20) { *output += 0x20; input -= 20; } -
if (input >= 10) { *output += 0x10; input -= 10; } -
*output += (unsigned char)(input & 0x0F); -
} -
-
unsigned char output[5]; -
-
int main(int argc, char* argv[]) -
{ -
-
bin2bcd(output,0xFFFFFFFF); -
bin2bcd(output,3999999999); -
bin2bcd(output,0); -
return 0; -
} -
-
//--------------end of file-----------------
16位BIN轉BCD碼
#include <REG52.H>
#define wufuhao_zifu unsigned char
void calculate(unsigned int value)
{
wufuhao_zifu tempA; //變量保存16位的高8位
wufuhao_zifu tempB; //變量保存16位的低8位
wufuhao_zifu temp1; //變量保存16位數據的個位
wufuhao_zifu temp2; //變量保存16位數據的十位
wufuhao_zifu temp3; //變量保存16位數據的百位
wufuhao_zifu temp4; //變量保存16位數據的千位
wufuhao_zifu temp5; //變量保存16位數據的萬位
tempA=value>>8;
tempB=value&0x00ff;
temp1=temp2=temp3=temp4=temp5=0;
while(((tempA==0x27)&&(tempB>=0x10))||(tempA>0x27))
{
if(tempB>=0x10)
{
tempB-=0x10;
tempA-=0x27;
}
else
{
tempB+=(~0x10)+1;
tempA-=0x27+1;
}
temp5++;
}
while(((tempA==0x03)&&(tempB>=0xe8))||(tempA>0x03))
{
if(tempB>=0xe8)
{
tempB-=0xe8;
tempA-=0x03;
}
else
{
tempB+=(~0xe8)+1;
tempA-=0x03+1;
}
temp4++;
}
while(((tempA==0)&&(tempB>=0x64))||(tempA>0))
{
if(tempB>=0x64)
{
tempB-=0x64;
}
else
{
tempB+=(~0x64)+1;
tempA-=1;
}
temp3++;
}
while(tempB>=0x0a)
{
tempB-=0x0a;
temp2++;
}
while(tempB>=0x01)
{
tempB-=0x01;
temp1++;
}
}
void main()
{
while(1)
{
calculate(65535);
}
}
---------------------------------------------------------------------------------------------------------------
非常快速的16位BIN轉BCD程序
#include <reg51.h>
unsigned char dig[5];
void bin2bcd(unsigned int x)
{
unsigned char i,j,k;
k = x;
x >>= 2;
i = x >> 8;
j = (i + i + i) << 1;
if (i > 41) {i++;j+=6;}
j += x;
if ((unsigned char)x > j ) {i++;j+=6;}
if (j > 249) {i++;j+=6;}
dig[0] = i / 10; //10000
dig[1] = B; //1000
dig[2] = j / 25; //100
dig[3] = (B<<2 | k&3) / 10; //10
dig[4] = B; //1
}
void main()
{
while(1)
{
bin2bcd(32768);
}
}
|