# 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 *)
(&gt;K:COM_RADIO_SET) </Click>

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*)
1000 &lt; (* ADF smaller 1000kHz need ADF_LOWRANGE_SET,
higher need ADF_HIGHRANGE_SET *)
if{ (&gt;K:ADF_LOWRANGE_SET) } els{
(&gt;K:ADF_HIGHRANGE_SET) }
</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?

Answer:

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
•
{"ft_lang_label":"__label__en","ft_lang_prob":0.6946982,"math_prob":0.98454344,"size":11793,"snap":"2021-21-2021-25","text_gpt3_token_len":3635,"char_repetition_ratio":0.10408007,"word_repetition_ratio":0.3569336,"special_character_ratio":0.35283643,"punctuation_ratio":0.14892685,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9957523,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-17T18:08:49Z\",\"WARC-Record-ID\":\"<urn:uuid:0346bcf8-6493-44a6-8e3d-44eb9159d799>\",\"Content-Length\":\"104689\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a2a8370b-4bae-4452-b76e-a77ee1558804>\",\"WARC-Concurrent-To\":\"<urn:uuid:2c2633f6-0319-4a07-835e-59eaee3f413e>\",\"WARC-IP-Address\":\"104.21.2.117\",\"WARC-Target-URI\":\"https://www.flightsim.com/vbfs/showthread.php?152228-Useful-Tips-Code-Samples-Tricks&s=11e7160ece7ef88436dd90457f3636fd\",\"WARC-Payload-Digest\":\"sha1:GRJNNEOCXDQNP5GZTV44VCJAJRZMQKFH\",\"WARC-Block-Digest\":\"sha1:ERZ2T5KSDMDDDZEIE6A5EZYU4LWE4CGV\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487630518.38_warc_CC-MAIN-20210617162149-20210617192149-00209.warc.gz\"}"}