Conversion in Advanced Ladder
See also: Project Toolbox for Advanced Ladder
Topic Menu
Conversion Operations Overview
The conversion operations in Cscape are used to translate data from one format to another. The result is always the same value but in a different data format. For example, Analog Inputs normally are in a raw, INT Integer - [Data Type INT] - A 16-bit signed value. Integers are used where the value of the data is expected to be in the range of -32,768 to +32,767. format and need to be scaled or put through some algorithm to determine what the sensor is indicating. The INT format, which does not support decimal places, can be converted to the REAL
These numbers use IEEE 754-1985 format to store numbers in following ranges.
32-bit single-precision floating point (REAL) – -3.40282E+38 to +3.40282E+38
64-bit double-precision floating point (LREAL) – -1.79769E+308 to +1.7976E+308
Floating Point refers to both REAL and LREAL data types. (aka Floating Point) data format before scaling or calculations take place so that precision and resolution is not lost.
Note: Entering Floating Point Values
All floating numbers must adhere to the following format:
These numbers use IEEE 754-1985 format to store numbers in following ranges.
32-bit single-precision floating point (REAL) – -3.40282E+38 to +3.40282E+38
64-bit double-precision floating point (LREAL) – -1.79769E+308 to +1.7976E+308
Floating Point refers to both REAL and LREAL data types.
If an exponent is included, the mantissa (value) portion must also contain a decimal point.
Note: If the entered format is other than x.yyy, the decimal point is moved and the exponent adjusted accordingly:
-
123.456e+3 = 123456 (The actual value can be displayed with six digits and no exponent)
-
143.643E-12 = 1.43643E-10 (Decimal point is moved and exponent adjusted)
A decimal point must be included to reduce any ambiguities. For example, 123e10 should be entered as 123.0e10, or better still 1.23e12 (Cscape will automatically convert to this format).
Neither the mantissa nor the exponent may contain spaces. "123 45e-12" and "4.3256e
-23" will not be interpreted correctly because of the embedded spaces.
Both the mantissa and the exponent may contain a sign, + or -; i.e.: "-1.3245e+12" or "4.243e-8". if the sign is missing then the associated part is assumed to be positive, "1.2345e10".
For any function in Cscape Advanced Ladder Logic, the data format of all inputs and any output must match each other (except for these Conversion Operations). The Conversion Operations are used to match these data formats.
Note: Currently, it is not possible to specify Unsigned Integer (UINT Unsigned Integer - [Data Type UINT] - A 16-bit unsigned value. Unsigned Integers are used where the value of the data is expected to be in the range of -0 (zero) to 65,535.) or Unsigned Double-Integer (UDINT
Unsigned Double Integer - [Data Type UDINT] - A 32-bit unsigned value. Unsigned Double Integers are used where the value of the data is expected to be in the range of 0 (zero) to 4,294,967,296.) formats in Conversion Operations. Integer values are always treated as Signed.
Smaller Formats to Larger Formats
Note: Does not include ASCII)
![]() |
![]() |
This converts the INT (16-bit) value in IN1 to a REAL (32-bit) value in Q.
Note: IN1 is a 16-bit value and Q is a 32-bit value.
![]() |
![]() |
This converts the INT (16-bit) value in IN1 to a LREAL (64-bit) value in Q.
Note: IN1 is a 16-bit value and Q is a 64-bit value.
![]() |
![]() |
Note: This converts the INT (16-bit) value in IN1 to a DINT (32-bit) value in Q.
IN1 is a 16-bit value and Q is a 32-bit value.
Configuration
Note: While a single INT to REAL example is provided here, all non-ASCII conversion operations have the same configuration.
-
IN: The Source of the data to convert. Must be a variable/register. For registers, the I/O name will be either the 16-bit, 32-bit, or 64-bit I/O name of the attached register depending on which Conversion Operation is being used.
-
Q: The Destination location in which the converted value will be placed. Must be a variable/register. For registers, the I/O name will be either the 16-bit, 32-bit, or 64-bit I/O name of the attached register depending on which Conversion Operation is being used.
Power Flow
-
The Conversion Operations execute immediately and entirely as soon as they receive power from the incoming rung. The resulting data in the destination (Q) is available for any other use immediately.
-
Conversions Operations will execute on every scan that they have received power from the incoming rung.
-
Conversions from smaller formats to larger formats will always pass power flow and give a valid result.
-
There is no need for any further functions to exist to the right of the Conversion Operations on the rung. The function’s result is considered an output.
Caveats: Precision may be lost in some cases. For instance, the REAL format supports 6 digits of precision and converting a DINT value of 1,234,567,890 to REAL format would result in 1.23456E+009. This is considered a successful conversion.
Example: Converting a 4-20mA or 0-10V input to a Floating Point reading (See Scaling Analog Inputs in General I/O Configuration ):
Return to the Top: Conversion in Advanced Ladder
Larger Formats to Smaller Formats
Note: Does not include ASCII)
![]() |
![]() |
This converts the REAL (32-bit) value in IN1 to a INT (16-bit) value in Q.
Note: IN1 is a 32-bit value and Q is a 16-bit value.
![]() |
![]() |
This converts the LREAL (64-bit) value in IN1 to a INT (16-bit) value in Q.
Note: IN1 is a 64-bit value and Q is a 16-bit value.
![]() |
![]() |
This converts the LREAL (64-bit) value in IN1 to a DINT (32-bit) value in Q.
Note: IN1 is a 64-bit value and Q is a 32-bit value.
![]() |
![]() |
This converts the DINT (32-bit) value in IN1 to a INT (16-bit) value in Q.
Note: IN1 is a 32-bit value and Q is a 16-bit value.
Configuration
Note: While a single REAL to INT example is provided here, all non-ASCII conversion operations have the same configuration.
-
IN: The Source of the data to convert. Must be a variable/register. For registers, the I/O name will be either the 16-bit, 32-bit, or 64-bit I/O name of the attached register depending on which Conversion Operation is being used.
-
Q: The Destination location in which the converted value will be placed. Must be a variable/register. For registers, the I/O name will be either the 16-bit, 32-bit, or 64-bit I/O name of the attached register depending on which Conversion Operation is being used.
Power Flow
-
The Conversion Operations execute immediately and entirely as soon as they receive power from the incoming rung. The resulting data in the destination (Q) is available for any other use immediately.
-
Conversions Operations will execute on every scan that they have received power from the incoming rung.
-
Conversions from larger formats to smaller formats will pass power flow and give a valid result if the result will fit in the Destination format.
-
Conversion Operations will NOT pass power flow or give any valid result if the conversion is not possible. For instance, conversions from DINT to INT where the value to convert is 40,000 is not possible since the value does not fit within the INT data format (-32,768 to +32,767).
-
There is no need for any further functions to exist to the right of the Conversion Operations on the rung. The function’s result is considered an output.
Caveats: Rounding is not performed when converting REAL/LREAL values with digits after the decimal place to formats that do not support decimal places, such as INT/DINT. Any data to the right of the decimal place is simply removed. This is considered a successful conversion.
Conversion |
Source Value |
Destination Value |
Power Flow |
---|---|---|---|
DINT to INT |
30000 |
30000 |
YES |
DINT to INT |
40000 |
N/A |
NO |
REAL to DINT |
3.14159E+004 |
31415 |
YES |
REAL to INT |
0.986 |
0 |
YES |
Return to the Top: Conversion in Advanced Ladder
Numeric Formats to ASCII
|
|
|
|
|
The Numeric to ASCII functions convert binary numeric values into ASCII representations of the number. These functions add a null at the end of the conversion thereby using one more byte than specified in the setup window. These functions (as shown above) will always pass power unless the IN1 values exceed the maximum number of characters.
Configuration
When converting from any data format to the ASCII format, additional configuration is provided to determine digits and number of decimal places in the result ASCII string, as well as whether the resulting ASCII string will be zero-filled in places that would normally be empty.
-
The function will use as much space as is needed beyond the designated Destination starting location to complete the function (each ASCII character is 8-bits in length)
-
The function will add a NULL
Null Termination - To place a NULL character (character code 0) at the end of ASCII data. Some functions require NULL Termination to be able to determine the end point of the ASCII data since that data may vary in length from one time to the next. character to the end of the Destination output, therefore occupying one character more than otherwise specified in the configuration.
-
Rounding will occur according to standard rounding rules (down if xxx.499999 or less, up if xxx.500000 or more)
-
When converting from REAL (floating point) format, see note below, the decimal position of the source number is used.
-
When converting from INT or DINT formats, the decimal is inserted in the position configured.
Note: While a single REAL to ASCII example is provided here, all conversions to the ASCII format have the same configuration.
-
IN: The Source of the data to convert. Must be a variable/register.
-
Q: The starting Destination location in which the converted value will be placed. Multiple variables/registers may be affected depending on the function configuration
-
Max Characters: The maximum number of characters allowed in the result. This includes the decimal place character but does NOT include the terminating NULL character placed at the end of the result.
-
Decimal Pos.: The position of any decimal place that is required. The location is specified starting from the right and counting left. A value of 0 will not use a decimal character anywhere in the result.
-
When converting from REAL (floating point) format, the decimal position of the source number is used.
-
When converting from INT or DINT formats, the decimal is inserted in the position configured.
-
-
Zero Filled: When this option is not checked, only the number of characters required for the conversion, up to the Max Characters specified, will be used. This can lead to results with varying character lengths. Check this option to zero-fill any otherwise unused leading characters in the result, therefore keeping the resulting output exactly the same as the Max Characters specified.
Power Flow
-
The Conversion Operations execute immediately and entirely as soon as they receive power from the incoming rung. The resulting data in the destination (Q) is available for any other use immediately.
-
Conversion Operations will execute on every scan that they have received power from the incoming rung.
-
Conversion Operations will pass power flow and give a valid result if the conversion is possible within the space defined for the result.
-
Conversion Operations will NOT pass power or have any valid result if the conversion is not possible within the space defined for the result. For instance, power flow would NOT occur if trying to convert a value of 5000 but only specifying Max Characters as 3.
-
There is no need for any further functions to exist to the right of the Conversion Operations on the rung. The function’s result is considered an output.
Examples for INT/DINT to ASCII conversions
Source Number |
Max Chars |
Dec Pos |
Zero Filled |
Resulting ASCII |
Resulting HEX character codes in Registers |
|||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
%R4 |
%R3 |
%R2 |
%R1 |
|||||||||
HI |
LO |
HI |
LO |
HI |
LO |
HI |
LO |
|||||
12345 |
6 |
2 |
NO |
123.45 |
[xx] |
[00] |
[35] |
[34] |
[2E] |
[33] |
[32] |
[31] |
12345 |
6 |
0 |
NO |
123.45 |
[xx] |
[xx] |
[00] |
[35] |
[34] |
[33] |
[32] |
[31] |
12345 |
6 |
1 |
YES |
123.45 |
[xx] |
[00] |
[35] |
[2E] |
[34] |
[33] |
[32] |
[31] |
Note: [xx] denotes a character location not affected by this operation
Examples for REAL/LREAL to ASCII conversions
Source Number |
Max Chars |
Dec Pos |
Zero Filled |
Resulting ASCII |
Resulting HEX character codes in Registers |
|||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
%R4 |
%R3 |
%R2 |
%R1 |
|||||||||
HI |
LO |
HI |
LO |
HI |
LO |
HI |
LO |
|||||
123.456489 |
6 |
2 |
NO |
123.46 |
[xx] |
[00] |
[36] |
[34] |
[2E] |
[33] |
[32] |
[31] |
123.456489 |
6 |
0 |
NO |
123 |
[xx] |
[xx] |
[xx] |
[xx] |
[00] |
[33] |
[32] |
[31] |
123.456489 |
6 |
1 |
YES |
0123.5 |
[xx] |
[00] |
[35] |
[2E] |
[33] |
[32] |
[31] |
[30] |
Source Number |
Max Chars |
Dec Pos |
Zero Filled |
Resulting ASCII |
Resulting HEX character codes in Registers |
|||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
%R4 |
%R3 |
%R2 |
%R1 |
|||||||||
HI |
LO |
HI |
LO |
HI |
LO |
HI |
LO |
|||||
12345 |
6 |
2 |
NO |
123.45 |
[xx] |
[00] |
[35] |
[34] |
[2E] |
[33] |
[32] |
[31] |
12345 |
6 |
0 |
NO |
12345 |
[xx] |
[xx] |
[00] |
[35] |
[34] |
[33] |
[32] |
[31] |
12345 |
6 |
1 |
YES |
1234.5 |
[xx] |
[00] |
[35] |
[2E] |
[34] |
[33] |
[32] |
[31] |
Return to the Top: Conversion in Advanced Ladder
ASCII to Numeric Formats
|
|
|
|
|
The ASCII to numeric functions convert an ASCII string to a INT, DINT, REAL or LREAL binary value. The input should be the starting register of an ASCII string representing a numeric value. This function will continue scan the ASCII characters until a non-numeric value is found. If the ASCII characters do not represent a legal numeric value, the output value is set to zero. These functions (as shown above) will always pass power.
Configuration
When converting from ASCII to any other data format, the function will operate on the Source data until a non-numeric ASCII character is reached. It is advised to terminate the Source ASCII data with a NULL character if there is any doubt about what characters may directly follow. Numeric characters are as follows:
ASCII Character | HEX Character Code | When Used |
---|---|---|
0-9 | 0x30 – 0x39 | Always |
- | 0x2D | Always |
. | 0x2E | REAL/LREAL Only, decimal places |
E | 0x45 | REAL/LREAL Only, Scientific Notation |
e | 0x65 | REAL/LREAL Only, Scientific Notation |
Note: The comma character, as used in larger numbers, i.e. 32,000, or used as a decimal place, i.e. 123,45, is not considered numeric.
Note: When converting to INT/DINT formats, conversion stops if a decimal place is detected and no rounding based on any decimal portion of the ASCII source will occur.
Caveats When Converting From ASCII:
-
The function will always give a result and will always pass power, regardless of the incoming ASCII data
-
When converting to INT/DINT formats, the result can be used correctly as either a signed value or unsigned value if the source ASCII falls within those respective data ranges.
-
The result will “wrap” in cases where the UINT/UDINT formats are exceeded. For example, an ASCII 65536 becomes INT 0, ASCII 65537 becomes INT 1, etc. This can be characterized by dividing the source value by the UINT/UDINT maximum value + 1, then using the remainder, aka perform a modulo function: INT Result = Converted Source mod 65536, or DINT Result = Converted Source mod 4,294,967,296.
Parameters
-
IN: The Source of the data to convert. Must be a variable/register.
-
Q: The Destination location in which the converted value will be placed. Must be a variable/register.
Power Flow
-
The Conversion Operations execute immediately and entirely as soon as they receive power from the incoming rung. The resulting data in the destination (Q) is available for any other use immediately.
-
Conversion Operations will execute on every scan that they have received power from the incoming rung.
-
Conversion Operations from ASCII will always pass power flow and give a result as noted in the caveats above.
-
There is no need for any further functions to exist to the right of the Conversion Operations on the rung. The function’s result is considered an output.
Examples for ASCII to INT conversions
Source to ASCII |
Resulting INT |
Resulting UINT |
Notes |
---|---|---|---|
"12345" |
12345 |
12345 |
|
"123.89" |
123 |
123 |
Decimal places “chopped”, not rounded |
"-12345" |
-12345 |
--- |
|
"45000" |
--- |
45000 |
|
"" (nothing) |
0 |
0 |
|
"70000" |
4464 |
4464 |
70000 mod 65536 = 4464 |
"AB5900" |
0 |
0 |
A” is non-numeric, function stops |
"125PSI" |
125 |
125 |
|
Examples for ASCII to REAL/LREAL conversions
Source ASCII |
Resulting REAL |
---|---|
"12345" | 12345 |
"123.89" | 123.89 |
"-1.2345" | -1.2345 |
".12345" | 0.12345 |
"" (nothing) | 0 |
"1.35E3" | 1350 |
1.35E-3 | 0.00135 |
"1.35EPI" | 1.35 |
"$22.00" | 0 |
“1000000000” | 1e+009 |
Return to the Top: Conversion in Advanced Ladder