|
|||||||||||
| 技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng) 驅(qū)動編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機(jī) | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計 | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe |
[分享]十進(jìn)制轉(zhuǎn)bcd三種算法 |
| 作者:liuli9230 欄目:單片機(jī) |
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); } } |
|
|
| 免費(fèi)注冊為維庫電子開發(fā)網(wǎng)會員,參與電子工程師社區(qū)討論,點(diǎn)此進(jìn)入 |
Copyright © 1998-2006 m.58mhw.cn 浙ICP證030469號 |