找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 9106|回复: 0
收起左侧

二进制转BCD码快速算法 bin to bcd fast code.

[复制链接]
ID:71407 发表于 2014-12-31 01:01 | 显示全部楼层 |阅读模式
                                                                                                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);
  }
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表