C: Decimal to BCD: Difference between revisions
From FSDeveloper Wiki
Jump to navigationJump to search
(New page: {{Infobox-Applicable-FSVersion | FSXI = false | FSXA = true | FSX = true | FS2004 = true | FS2002 = false | FS2000 = unknown | FS98 = unknown }} == C: BCD to Decimal and Decimal to BCD == ...) |
No edit summary |
||
| Line 4: | Line 4: | ||
| FSX = true | | FSX = true | ||
| FS2004 = true | | FS2004 = true | ||
| FS2002 = | | FS2002 = true | ||
| FS2000 = | | FS2000 = true | ||
| FS98 = | | FS98 = true | ||
}} | }} | ||
== C: BCD to Decimal and Decimal to BCD == | == C: BCD to Decimal and Decimal to BCD == | ||
Revision as of 09:43, 13 June 2012
C: BCD to Decimal and Decimal to BCD
This scheme was supplied by Arne Bartels which allows BCD conversion in both directions. Add the following to the master source file:-
#include <math.h>
#define Bcd2Dec(BcdNum) HornerScheme(BcdNum,0x10,10)
#define Dec2Bcd(DecNum) HornerScheme(DecNum,10,0x10)
UINT32 HornerScheme(UINT32 Num,UINT32 Divider,UINT32 Factor)
{
UINT32 Remainder=0,Quotient=0,Result=0;
Remainder=Num%Divider;
Quotient=Num/Divider;
if(!(Quotient==0&&Remainder==0))
Result+=HornerScheme(Quotient,Divider,Factor)*Factor+Remainder;
return Result;
}
Then for each sub-gauge that requires a BCD conversion use the following syntax:-
Bcd2Dec(bcd_number) for BCD to decimal and Dec2Bcd(dec_number) for decimal to BCD conversions.