GetUtf8StringLength
Short summary
This functions returns the length of an UTF-8 string in bytes and the number of unicode characters.
Attention: All strings are handled as null-terminated byte streams
Example:
myString :String(10) := 'Test';
bytecount, charCount :UDINT;
------------------
GetUtf8StringLength(
stringAddress := ADR(myString),
byteCount => byteCount,
characterCount => charCount
);
- Return type:
UDINT
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| stringAddress | POINTER TO BYTE | - | input |
| byteCount | UDINT | length of string in bytes | output |
| characterCount | UDINT | unicode characters in the string | output |
Code
Declaration
FUNCTION GetUtf8StringLength :UDINT;
VAR_INPUT
stringAddress :POINTER TO BYTE;
END_VAR
VAR CONSTANT
(* the end of string marker *)
END_OF_STRING :BYTE := 16#00;
END_VAR
VAR_OUTPUT
(* length of string in bytes *)
byteCount :UDINT := 0;
(* unicode characters in the string *)
characterCount :UDINT := 0;
END_VAR
VAR
octetsCount :BYTE(0..4);
END_VAR
Implementation
GetUtf8StringLength := 0;
RETURN(stringAddress = 0);
WHILE (stringAddress[byteCount] <> END_OF_STRING) DO
characterCount := characterCount + 1;
octetsCount := GetOctetCountByFirstOctet(stringAddress[byteCount]);
IF (octetsCount > 0) THEN
byteCount := byteCount + octetsCount;
ELSE
Tc2_System.ADSLOGSTR(
msgCtrlMask := Tc2_System.Global_Variables.ADSLOG_MSGTYPE_ERROR,
msgFmtStr := 'CNM_UnicodeUtilities: Invalid UTF-8 encoding found! First byte: %s',
strArg := Tc2_Utilities.BYTE_TO_HEXSTR(in := stringAddress[byteCount], iPrecision := 2)
);
characterCount := 0;
byteCount := 0;
GetUtf8StringLength := 0;
RETURN;
END_IF
END_WHILE
GetUtf8StringLength := byteCount;