public static void serializeS7String(WriteBuffer io, PlcValue value, int stringLength, String encoding) {
        int maxStringLength = 0xFF & Math.min(stringLength, 250);
        int actStringLength = 0xFF & value.getString().length();
        actStringLength = Math.min(maxStringLength, actStringLength);

        switch (encoding) {
            case "UTF-8": {
                byte[] chars = new byte[maxStringLength];
                byte[] actChars = value.getString().substring(0, actStringLength).getBytes(StandardCharsets.UTF_8);
                System.arraycopy(actChars, 0, chars, 0, actChars.length);
                try {
                    io.writeUnsignedInt(8, maxStringLength);
                    io.writeUnsignedInt(8, actStringLength);
                    io.writeByteArray(chars);
                } catch (SerializationException ex) {
                    Logger.getLogger(StaticHelper.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            }
            case "UTF-16": {
                byte[] chars = new byte[maxStringLength * 2];
                byte[] actChars = value.getString().substring(0, actStringLength).getBytes(StandardCharsets.UTF_16BE);
                System.arraycopy(actChars, 0, chars, 0, actChars.length);
                try {
                    io.writeUnsignedInt(16, maxStringLength);
                    io.writeUnsignedInt(16, actStringLength);
                    io.writeByteArray(chars);
                } catch (SerializationException ex) {
                    Logger.getLogger(StaticHelper.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            }
            default:
                throw new PlcRuntimeException("Unsupported encoding: " + encoding);
        }

    }
Symbol       Meaning
======       =======

@param alarm     Alarm type from PLC.
@param alarmText The text string to be processed.
@param textlists List of texts for indexed replacement.
@return The text string with the replacement values.
@ Beginning of format string
Pv           Process value number (optional)
Typ          Type of process value (optional)
% width      Width of the signaling field on the OP
.precision   Number of decimal places (optional with format = "f")
format       Process value representation mode
@ End of format string
<p>
Type		Meaning
====             =======
B		BOOL
Y		BYTE
C		CHARACTER
W		WORD
I		INTEGER
X		DWORD
D		DINT
R		REAL
<p>
Representation       Format
==============       ======
%[i]d                Decimal signed
%[i]u                Decimal without sign
%[i]x                Hexadecimal
%[i].[y]f            Signed fixed point
%[i]b                Binary
%[i]s                Character string (STRING ANSI)
%t#<Library name>    Access to text library
<p>
General representation:
@@&gt;Associated Value>&gt;Type>&gt;Format>&gt;Library name>@
plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/utils/StaticHelper.java