This application note discusses how the long signed decimaland long unsigned decimal format specifiers are used with the str_Printf(…) function. This application note is intended for use in the Workshop 4 – Designer environment. The 4DGL code of the Designer project can be copied and pasted to an empty ViSi project and it will compile normally. The code can also be integrated to that of an existing ViSi project.

Description

There are four decimal format specifiers:

This application note discusses how the long signed decimal and long unsigned decimal format specifiers are used with the str_Printf(…) function. This application note is intended for use in the Workshop 4 – Designer environment. The 4DGL code of the Designer project can be copied and pasted to an empty ViSi project and it will compile normally. The code can also be integrated to that of an existing ViSi project.

When downloading an application note, a list of recommended application notes is shown. It is assumed that the user has read or has a working knowledge of the topics presented in these recommended application notes.

Application Overview

The application note Designer or ViSi Strings and Character Arrays explains how 4DGL strings and character arrays are stored in and accessed from memory. It also differentiates between word-aligned and byte-aligned pointers. Furthermore, it introduces the use of the function str_Printf(…).

Design the Project

The Format Specifier “%ld”

The format specifier “%ld” is used for displaying long signed decimal numbers. A long signed decimal in 4DGL is a signed 32-bit (or 4-byte) integer value, the range of which is from -2,147,483,648 to 2,147,483,647(-231 to 231-1). Consider the code snippet shown below.

The output of the above code is:

The function umul_1616(…) performs an unsigned multiplication of two 16-bit values, placing the 32-bit result in a two-word array. In this example, the two 16-bit values are 500 and 2000. When multiplied together the product of these is 1000000. If we print the contents of the word array val32 in hexadecimal format,

we get,

We analyse the contents of the word array val32.

Note also that the pointer was advanced by four bytes after the long signed decimal value was printed.

Therefore, the long signed decimal format specifier, “%ld”, causes the str_Printf(…) function to get four bytes from the address starting at that pointed to by the byte-aligned pointer. str_Printf(…) then treats these four bytes as a 32-bit signed integer and prints the decimal equivalent.

The 32-bit data found starting at address 16, in this example, is “0x000F 4240”, the decimal equivalent of which is “1000000”.

The Width and Zero Flag Sub-specifiers

The Width Sub-specifier

Consider the output below.

The code for the first line is

The code for the second line is

Note that in the second line, the number has three spaces preceding it. This is because the width specifier was used in the str_Printf(…) function.

Here the width specifier value is 10, so the field width of the printed figure is ten digits, and since the number is only seven digits, it is preceded by three space characters.

The Zero Flag Sub-specifier

Suppose we want the number to be preceded with zeros rather than spaces, we would write,

Here the width sub-specifier is preceded by the zero flag sub-specifier, which would cause the number to be left-padded with zeros instead of spaces. To illustrate,

Therefore, without the zero flag sub-specifier, the default character with which a number, printed with a certain field width, is to be left-padded is the space character. The width and zero flag sub-specifiers can be used with other format specifiers besides the long signed decimal format specifier.

The Designer project for the discussions on the long signed decimal format specifier and the width and zero flag sub-speficiers is “stringsBasics6.4dg” (attached).

Printing Negative Numbers

Print a Negative Long Decimal Number

To print a negative long decimal number, we first generate a negative 32-bit integer by multiplying the two 16-bit integers shown below.

Again, the function umul_1616(…) performs an unsigned multiplication of two 16-bit values placing the 32-bit result in a two-word array. In this example, the two 16-bit values are 0xFF12 and 0xFF34. When treated as unsigned numbers and multiplied together, the product of these is 0xFE46 BDA8. If we print the contents of the word array val32 in hexadecimal format,

we get,

Using the long signed decimal format specifier, “%ld”, would cause str_Printf(…) to treat the two-word data as a signed 32-bit integer. Hence, we get the decimal equivalent value of “-28918360” for “0xFE46 BDA8”.

Print a Long Unsigned Decimal Number

To make str_Printf(…) treat the data inside val32[0] and val32[1] as an unsigned 32-bit integer, we use the long unsigned decimal format specifier “%lu”.

Hence we get the equivalent decimal value “4266048936”.

Dynamic Construction of the Format Specifier

As was shown in the application note Designer or ViSi 4DGL Strings Print Formats – the String and Character Format Specifiers, the format specifier argument of the str_Printf(…) function can also be a word-aligned string pointer, allowing dynamic construction of the printing format. We will now use dynamically constructed format specifiers, together with the width and zero flag sub-specifiers, to come up with the formatted display output shown below.

The code snippet for the above output is:

The Designer project for the remaining part of this application note is “stringsBasics7b.4dg” (attached). Although the examples are simple, the ability to construct a format specifier dynamically can be a powerful tool.

Run the Program

For instructions on how to save a Designer project, how to connect the target display to the PC, how to select the program destination, and how to compile and download a program, please refer to the section “Run the Program” of the application note

For instructions on how to save a ViSi project, how to connect the target display to the PC, how to select the program destination, and how to compile and download a program, please refer to the section “Run the Program” of the application note