十进制转bcd三种算法的比较

十进制转bcd三种算法的比较

//
*********************************************************************
************

// *   Function: DecimalToBcdAscii
*
// * Description: Convert
decimal value to 3 digit BCD ASCII value *
//
*******************************************************************************
**
unsigned char Hundreds,Tens,Ones;
void DecimalToBcdAscii(signed short DecimalValue)
{
  Hundreds = 0;
// Initialize BCD values
  Tens = 0;
  Ones = 0;

  Hundreds:
// Hundreds
  DecimalValue = DecimalValue - 100;
    if (DecimalValue < 0)  
    {
      goto Tens1;
      }
    Hundreds = Hundreds + 1; // Increment
Hundreds count
    goto Hundreds;
  Tens1:
// Tens
  DecimalValue = DecimalValue + 100;
  Tens2:
    DecimalValue = DecimalValue - 10;
    if (DecimalValue < 0)  
    {
      goto Ones1;
      }
    Tens = Tens + 1; //
Increment Tens count
    goto Tens2;  
  Ones1:
// Ones
  DecimalValue = DecimalValue + 10;
  Ones2:
    DecimalValue = DecimalValue - 1;
    if (DecimalValue < 0)  
    {
      goto AddAsciiOffset;
      }
    Ones = Ones + 1; //
Increment Ones count
    goto Ones2;  
  AddAsciiOffset:
// Add ASCII offset
    Hundreds = Hundreds + 48;
    Tens = Tens + 48;
    Ones = Ones + 48;
}
#include<reg51.h>
#include<stdio.h>
main()
{//clk=12Mhz
int i;
//算法1 耗时0.25s
for (i=0;i<1000;i++)
DecimalToBcdAscii(i);
//算法2 耗时0.42s
for (i=0;i<1000;i++)
{int t;
Hundreds=i/100;
t=i%100;
Tens=t/10;
Ones=i%10;
}
Hundreds = Hundreds + 48;
Tens = Tens + 48;
Ones = Ones + 48;
//算法3 耗时1.5s
for (i=0;i<1000;i++)
{unsigned char buf[5];
sprintf(buf,"%3d",i);
}
}

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值