WORD into STRING
The function WORD_TO_STRING converts a value of the data type WORD into a value of the data type STRING.
Generates a result string in right-aligned decimal representation, filled with leading spaces up to the predefined maximum number of characters.
Input
Output
When using the data type STRING with small PLCs like FP-e or FP0, make sure that the length of the result string is equal to or greater than the length of the source string.
Input |
Output defined as |
Results in |
---|---|---|
16#ABCD |
STRING[1] |
'D' |
STRING[2] |
'CD' |
|
STRING[3] |
'BCD' |
|
STRING[4] |
'ABCD' |
|
STRING[5] |
'0ABCD' |
|
STRING[6] |
'00ABCD' |
|
and so on... |
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
input_value: WORD:=0;
result_string: STRING;
The input variable input_value of the data type WORD is intialized by the value 16#ABCD. The output variable result_string is of the data type STRING[6]. It can store a maximum of 6 characters. Instead of using the variable input_value, you can enter a constant directly at the function’s input contact in the body.
The input_value of the data type WORD is converted into STRING[6]. The converted value is written to result_string. When the variable input_value = 16#ABCD, result_string shows '00ABCD'.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 2 ;
NETWORK_BODY
B(B_F,WORD_TO_STRING!,Instance,11,0,21,2,,?D?C);
B(B_VARIN,,input_value,9,0,11,2,);
B(B_VAROUT,,result_string,21,0,23,2,);
L(1,0,1,2);
END_NETWORK_BODY
END_NET_WORK
END_BODY
result_string:=WORD_TO_STRING(input_value);
This example illustrates how you create STRING[4] out of the data type WORD in which the leading part of the string '16#' is cut out.
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
input_value: WORD:=16#1234;
(*example value*)
result_string1: STRING[7]:='';
(*result: here '0001234'*)
result_string: STRING[4]:='';
(*result: here '1234'*)
END_VAR
In this example, both an input variable input_value of the data type WORD and an output variable result_string of the data type STRING[4] are declared.
In carrying out the operation in question, the standard function RIGHT is attached to the function WORD_TO_STRING. RIGHT creates a right-justified character string of length L.
In the example, the output string of WORD_TO_STRING function is added at the input of the RIGHT function. At the L input of RIGHT, the INT constant 4 determines the length of the STRING to be replaced. Out of the variable input_value = 0001234, the result_string 1234 results from the data type conversion and the RIGHT function.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 2 ;
NETWORK_BODY
B(B_F,WORD_TO_STRING!,Instance,11,0,21,2,,?D?C);
B(B_VARIN,,input_value,9,0,11,2,);
B(B_VAROUT,,result_string1,21,0,23,2,);
L(1,0,1,2);
END_NETWORK_BODY
END_NET_WORK
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 5 ;
NETWORK_BODY
B(B_VARIN,,result_string1,10,2,12,4,);
B(B_VAROUT,,result_string,17,2,19,4,);
B(B_F,RIGHT!,Instance,12,1,17,5,,?DIN?DL?C);
B(B_VARIN,,4,10,3,12,5,);
L(1,0,1,5);
END_NETWORK_BODY
END_NET_WORK
END_BODY