# Thread: Useful Tips, Code Samples & Tricks

1. ## Useful Tips, Code Samples & Tricks

Here is a collection of useful Tips, Code Samples and Tricks for gauge development... 2. ## BCD to Decimal Conversion Macro for C Gauges

Cut-and-Paste the following code to your gauges.h file, or save it as BCD2DEC.h, place it in your \inc folder and then #include it somewhere in your gauge project.

/*--------------------------------------*/
#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;
}
/*--------------------------------------*/

Usage is quite simple...

To convert the FS COM or NAV variable to Decimal:

COM1freq = Bcd2Dec(COM_FREQUENCYvar.var_value.n);
COM1freq = COM1freq/100 + 100;

To convert a decimal frequency to BCD:

COM1_bcd = (COM_dec - 100) * 1000;
COM1_bcd = Dec2Bcd( COM1_dec); 3. ## Displaying Text in Gauges

Reposted here from http://blogs.technet.com/engauged/default.aspx

Displaying Text in Gauges
Formatting numbers

To display the value of a sim variable in text, use the following syntax:

<String>%(VARIABLE NAME)%!XY!</String>

Where:

VARIABLE NAME is any of the variables listed in parameters.doc. Be sure to include parentheses around the variable name in addition to the parentheses included with the %’s. (see example below) Note that there must not be a space between the ‘%’ and the ‘!’.

Y (required, case-sensitive) is the formatting of the variable, where:

* s = string.
* d = number (integer). If the number is not an integer, it is rounded to the nearest integer. Note that rounding, not truncation occurs.
* f = number (floating point)

X is the minimum number of digits to display (optional)

If Y=’d’, the following rules apply:

* If X is preceded by the digit ‘0’, then leading zeros are added if necessary.
* If X is preceded by ‘-‘, text is left-aligned
* If X is preceded by ‘+’, a ‘+’ is indicated in front of the number when the number is greater than 0 (a ‘-‘ is always used to indicate numbers less than 0)
* If X is preceded by ‘ ‘ (space), leading spaces are added if necessary.

If Y=’f’, the following rule applies:

* If a decimal point is used in the number X, the digit after the decimal point specifies the number of digits to display after the decimal point. See examples below,

Script Result

%( 12.34 )%!4.3f! 12.340

%( 12.34 )%!04.3f! 12.340 (Leading ‘0’s not added to floating point numbers)

%( 12345.6789 )%!4.3f! 12345.679 (Number before decimal point does not limit the number of digits displayed before decimal point)

%( 34.56 )%!+d! +35 (Note that rounding, not truncation, occurs)

%(234)%!5d! 234 (note 2 leading spaces)

%( ‘foo’ )%!5s! foo (note 2 leading spaces)

%( 234 )%!3s! 234

Using Sim variables and expressions in text

For example:

<String>%((A:Indicated Altitude, feet))%!05f!</String>

Any of the operators described above (Boolean, string, arithmetic, etc.) may be used within the %() to create the intended expression. In other words,

<String>%(EXPRESSION)%!XY!</String>

Is valid, where EXPRESSION is any combination of valid sim variables and operators.

For example, the following string will display the current altitude in hundreds of feet or hundreds of meters, depending on the current international settings,

<String>
%( (P:Units of measure, enum) 2 ==
if{ (A:Indicated Altitude, meters) }
els{ (A:Indicated Altitude, feet) }
100 / )%!5d!
</String>

Keep in mind that the <String> element must be a child of a <Text> element, for example:

<Element>
<Position X="0" Y="175"/>
<Text
X="80" Y="20"
Bright="Yes"
Length="10"
Font="Quartz"
Color="#E0E0E0" >
String>05d: %((P:Local time,hours) flr)%!02d!</String>
</Text>
</Element> 4. ## BCD to Decimal Conversion Macro for C Gauges

EDITOR'S NOTE: Jean-Luc from Reality-XP has kindly offered this alternate method for BCD-to-DEC, DEC-to-BCD, and BCD-to-TXT conversions

Here is a simpler way to convert an FS BCD to a
string (the same can easily be used from FS BCD -> number)

void RXP_FREQBCD2TXT(char * str, UINT32 value)
{
str='1';
str=((value >> 12) & 0xF) + 0x30;
str=((value >> 8) & 0xF) + 0x30;
str='.';
str=((value >> 4) & 0xF) + 0x30;
str=((value) & 0xF) + 0x30;
str=0;
}

Here is an example with INT instead CHAR * (and I'm sure this
can be further optimized)

UINT32 RXP_6DEC2FSBCD(UINT32 val)
{
UINT32 bcd;

val /=10;
bcd = (val % 10);
val /= 10;
bcd |= ((val % 10) << 4);
val /= 10;
bcd |= ((val % 10) << 8);
val /= 10;
bcd |= ((val % 10) << 0x0C);
return(bcd);
}

UINT32 RXP_FSBCD26DEC(UINT32 bcd)
{
UINT32 decval;

decval = 100000;
decval +=((bcd >> 12) & 0xF) * 10000;
decval +=((bcd >> 8) & 0xF) * 1000;
decval +=((bcd >> 4) & 0xF) * 100;
decval +=((bcd) & 0xF) * 10;

if (((bcd & 0xF) == 2) || ((bcd & 0xF) == 7)) decval
+= 5;

return(decval);
} 5. ## BCD to Decimal Conversion for XML Gauges

/!-- COM or NAV Frequency Decimal to BCD --/

<Click> 123.45 (*any frequency here*)
100 * 10000 % int (* 123.45 to 2345 *)
d 10 % r 10 / int (* stack: 234 -> 5 *)
d 10 % r 10 / int (* stack: 23 -> 4 -> 5 *)
d 10 % r 10 / int (* stack: 2 -> 3 -> 4 -> 5 *)
16 * + (* stack: 2*16+3 -> 4 -> 5 *)
16 * + (* stack: (2*16+3)*16+4 -> 5 *)
16 * + (* stack: ((2*16+3)*16+4)*16+5 *)

For BCD to Decimal, no 'tricks' are needed, since the 'units'
parameter of the (A:variable,units) will do the conversion
for you... ;)

/!-- ADF Frequency Decimal to Octal --/

<Click> 1234.5 (*or any frequency here*)
d (* duplicate for later use *)
10 * 10000 % int (* 1234.5 to 2345 *)
d 10 % r 10 / int (* stack: 234 -> 5 *)
d 10 % r 10 / int (* stack: 23 -> 4 -> 5 *)
d 10 % r 10 / int (* stack: 2 -> 3 -> 4 -> 5 *)
16 * + (* stack: 2*16+3 -> 4 -> 5 *)
16 * + (* stack: (2*16+3)*16+4 -> 5 *)
16 * + (* stack: ((2*16+3)*16+4)*16+5 *)
r (* get the original frequency back*)
</Click>

(Editor's Note: Thanks to Arne Bartels for the cogent examples!) 6. ## Modulus Operator "%"

The following is applicable to both C and XML programming... ;)

Question:
>I understand modulus to a degree, like
>123 10 % strips out the 3 and places it on the stack.
>
>what does, say, 2 % do?

The "result" is only obvious whenever you use 10 as a divisor, because it effectively bitshifts the decimal, and the remainder is simply the decimal portion! ;)

With other numbers as a divisor however, the operation is more opaque... :9

The modulus, or remainder, operator divides number1 by number2 and returns only the remainder as result. The sign of result is the same as the sign of number1. The value of result is between 0 and the absolute value of number2.

For example, in the following expression, A (which is result) equals 5.6.

A = 19 6.7 %

This is equivalent to:

19 / 6.7 = 2.8358208

We throw away the fraction leaving 2 as the result

Now we have:

A = 19 - (6.7 * 2)

Therefore A = 5.6

The above is an example where the result is certainly not obvious! ;)

The "%" (or "mod") operator in computer languages is simply the remainder. For
example,

17 3 % = 2

because

17 / 3 = 5 rem 2

which in turn means

17 = 3 * 5 + 2

There are some tricky issues when negative numbers are used, but that
shouldn't ordinarily be necessary.

In math (number theory), the term is used a little differently. The
"modulus" is actually not the remainder, but the number you are
dividing by; and "mod" is not an operator, but a label telling "in
what sense two quantities are considered congruent, or equal." For
example, we would say

17 = 11 (mod 3)

(read as "17 is congruent to 11, modulo 3"), meaning that 17 and 11
both leave the SAME remainder when divided by 3. You probably won't
see this usage if you are only reading about programming, but it's
worth being aware of if you look deeper into the math behind it.

The expression a = b (% n) means that n is a divisor of a - b. This
sentence is read, "a is congruent to b modulo n." It is something
like a remainder, because if you subtract a remainder from the
dividend, the divisor will go into the result evenly.

Examples: 100 = 86 (% 7), because 100 - 86 = 14 has 7 as a divisor.
On the other hand, if you divide 100 by 7, the quotient is 14 and
remainder is 2, and 100 = 2 (% 7), too.

Now, back to your original question, what would 123 2 % be?

A = 123 2 %

123 / 2 = 61.5, therefore:

A = 123 - (61 * 2)

A = 1

:9 #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•