String Conversion — DTOS, STOD, RTOS, STOR, UPPER, LOWER
The Structured Text conversion instructions in rungs.dev turn numbers into text and text into numbers, and change the case of letters, each as a statement of its own.
A display shows text, and a barcode reader or a serial device sends text. Your logic works with numbers. These instructions cross between the two, and change the case of letters so two pieces of text can be compared whatever case they were typed in.
Each one is a statement of its own that writes its result into its last operand. None of
them gives back a value, so Speed := STOD(Reply); is refused: write STOD(Reply, Speed);.
DTOS(PartCount, CountText); (* -42 gives '-42' *)
STOD(Reply, Speed); (* '1500 rpm' gives 1500 *)
RTOS(Temperature, TempText); (* 21.5 gives '21.5000000' *)
STOR(Reply, Pressure); (* 'P=2.75bar' gives 2.75 *)
UPPER(Typed, Code); (* 'ab-12x' gives 'AB-12X' *)
LOWER(Typed, Code); (* 'AB-12X' gives 'ab-12x' *)Operands
| Instruction | Source | Destination |
|---|---|---|
DTOS | a DINT, INT, SINT or REAL | a STRING |
STOD | a STRING | a DINT, INT or SINT |
RTOS | a REAL | a STRING |
STOR | a STRING | a REAL |
UPPER | a STRING | a STRING |
LOWER | a STRING | a STRING |
A source is a tag, or for DTOS and RTOS a number typed in, never an expression:
DTOS(Count + 1, CountText); is refused, so work Count + 1 out into a tag first. Text in
quotes goes only in := and in a comparison, never into these.
What each one writes
DTOSmakes aREALa whole number first, the way:=stores aREALinto aDINT: a half goes to the even neighbour, so2.5gives'2'and3.5gives'4'. It clears the whole destination before it writes.STODskips everything up to the first digit, then reads digits until something that is not one. A-counts only directly before the first digit, so'SPD=-20'gives-20and'12.7'gives12. No digit at all gives0. A number too large for aDINTis held to theDINTlimit, with a warning; into anINTor aSINTit then wraps as any store does.RTOSalways writes nine digits: eight decimals from0.0001up to1, nine digits in all from1up to10000000, and nine digits with an exponent otherwise, as in'1.23456780e+07'. Zero gives'0.0'. The digits are theREAL's own, so123.456gives'123.456001'.STORreads likeSTOD, then a decimal point and more digits, and an exponent such ase3. Text starting0xreads as hexadecimal:'0x1F'gives31.0. A number aREALcannot hold givesInfinity, with a warning.UPPERandLOWERchange only the lettersatozandAtoZ.
When the source's LEN is outside 0 to 82, none of them reads it: UPPER and LOWER leave
the destination as it was, STOD and STOR store 0, and Studio shows a warning.
Common Mistakes
- Expecting
STORto read'.5'as0.5. It starts at the first digit, so it reads5. - Expecting
STODto round.'12.7'gives12. UseSTORand then:=into aDINT.
Related
- String instructions — join, find and cut text
- String conversion in Ladder
String Instructions — CONCAT, FIND, MID, INSERT, DELETE
The Structured Text string instructions in rungs.dev join STRINGs, find text inside one, and take part of one out, each as a statement of its own.
Simple Examples and Patterns
Reusable Structured Text patterns and worked examples in rungs.dev — motor start/stop, latching, edge detection, scaling, and other common PLC programming tasks.