P4034

Code

P4034

Message

Cannot mix STRING and WSTRING without an explicit conversion

This error occurs when a STRING value and a WSTRING value are mixed without an explicit conversion. STRING stores single-byte characters (Latin-1) and WSTRING stores double-byte code units (UTF-16LE). The two types have different in-memory encodings, so IEC 61131-3 provides no implicit conversion between them. Assigning or comparing one against the other is a type error.

Example

The following code will generate error P4034:

PROGRAM main
VAR
    narrow : STRING[10];
    wide : WSTRING[10];
END_VAR
    narrow := wide;
END_PROGRAM

The assignment narrow := wide copies a WSTRING into a STRING, which would reinterpret UTF-16LE bytes as Latin-1 characters.

The same applies to the operands of one operation. CONCAT(narrow, wide), FIND(narrow, wide) and narrow = wide each need a single encoding to work in, and there is none both operands share.

A string literal is typed by its quotes: 'abc' is a STRING and "abc" a WSTRING. So wide = 'abc' mixes the two types the same way wide = narrow does, and is the same error; write wide = "abc".

To fix this error, use variables of the same string type on both sides:

PROGRAM main
VAR
    narrow : STRING[10];
    wide : WSTRING[10];
    other : WSTRING[10];
END_VAR
    wide := other;
END_PROGRAM

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.