String Conversion — DTOS, STOD, RTOS, STOR, UPPER, LOWER

The Ladder Logic conversion instructions in rungs.dev turn numbers into text and text into numbers, and change the case of letters, for displays, barcodes and serial replies.

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: DTOS and RTOS write a number as text, STOD and STOR read a number out of text, and UPPER and LOWER change the case of letters so two pieces of text can be compared whatever case they were typed in.

Each one runs on every scan its rung is true, and does nothing on a false rung. Each one writes its result into its last operand.

DTOS — Number to Text

DTOS

Source

source

Dest

dest

Writes the Source number as decimal text into the destination STRING.

NameType
sourceDINT, INT, SINT or REAL
destSTRING

0

ShowCount

DTOS

Source

PartCount

Dest

CountText

With PartCount holding -42, CountText becomes '-42'. A REAL is made a whole number first, the way MOVE stores a REAL into a DINT: a half goes to the even neighbour, so 2.5 gives '2' and 3.5 gives '4'. DTOS clears the whole destination before it writes, so nothing of the old text is left in it.

STOD — Text to Whole Number

STOD

Source

source

Dest

dest

Reads the first whole number in the Source STRING and stores it in the destination.

NameType
sourceSTRING
destDINT, INT or SINT

0

ReplyReady

STOD

Source

Reply

Dest

Speed

STOD skips everything up to the first digit, then reads digits until something that is not a digit. A - counts only when it sits directly before the first digit:

sourcedest
'1500 rpm'1500
'SPD=-20'-20
'12.7'12
'- 5'5
'no digits'0

A number too large for a DINT is held to the DINT limit, 2147483647 or -2147483648, and Studio shows a warning. Into an INT or a SINT the number then wraps as any store does, so '300' into a SINT gives 44.

RTOS — REAL to Text

RTOS

Source

source

Dest

dest

Writes the Source REAL as text into the destination STRING.

NameType
sourceREAL
destSTRING

0

ShowTemp

RTOS

Source

Temperature

Dest

TempText

With Temperature holding 21.5, TempText becomes '21.5000000'. The text always has nine digits, written one of three ways:

sourceWritten asExample
0.0001 up to 1eight decimals0.25 gives '0.25000000'
1 up to 10000000nine digits in all1234.5 gives '1234.50000'
anything larger or smallernine digits, an exponent12345678.0 gives '1.23456780e+07'

Zero gives '0.0', and so does a number too close to zero for a REAL to hold normally. The digits are the REAL's own, so 0.1 gives '0.10000000' but 123.456 gives '123.456001': a REAL holds about seven digits exactly.

STOR — Text to REAL

STOR

Source

source

Dest

dest

Reads the first number in the Source STRING and stores it in the destination REAL.

NameType
sourceSTRING
destREAL

0

ReplyReady

STOR

Source

Reply

Dest

Pressure

Like STOD, STOR starts at the first digit, with a - directly before it. It then reads a decimal point and more digits, and an exponent such as e3 or E-2. So 'P=2.75bar' gives 2.75 and '1.5e3' gives 1500.0. Text starting 0x reads as hexadecimal: '0x1F' gives 31.0. A number larger than a REAL holds, or closer to zero than one holds, gives Infinity and a warning.

UPPER and LOWER — Change Case

UPPER

Source

source

Dest

dest

Copies the Source STRING into the destination STRING with its letters in upper case.

LOWER

Source

source

Dest

dest

Copies the Source STRING into the destination STRING with its letters in lower case.

NameType
sourceSTRING
destSTRING

0

CodeReady

UPPER

Source

Typed

Dest

Code

With Typed holding 'ab-12x', Code becomes 'AB-12X'. Only the letters a to z and A to Z change; digits, spaces and every other character stay as they are. To compare text whatever case it was typed in, change both sides to the same case and compare those.

A STRING with a broken LEN

When the source's LEN is outside 0 to 82, none of these instructions reads it. UPPER and LOWER leave dest as it was, STOD and STOR store 0, and Studio shows a warning that names the STRING.

Common Mistakes

  • Expecting STOR to read '.5' as 0.5. It starts at the first digit, so it reads 5. Write '0.5'.
  • Expecting STOD to round. '12.7' gives 12: it stops at the decimal point. Use STOR, then MOVE the REAL into a DINT to round it.
  • Putting text in quotes into a conversion, such as STOD('12',Speed). Put the text in a STRING tag and use the tag.

On this page