HTP. - HTTP Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Internet and Network > HTP. - Command > !HTP-General - General HTTP operations and utilities > !Utility - Helper functions for HTTP operations (string conversion, proxy toggling) >

HTP. - HTTP Operations

HTP.FromUTF8

Previous Top Next


SPR Script Language

 

HTP.FromUTF8

Validates and normalizes a string that is expected to be in UTF-8 format.

 

Intention

 

The HTP.FromUTF8 command ensures that a string contains valid UTF-8 byte sequences. It performs a round-trip conversion: the input string is first converted to the system's internal Unicode format (UTF-16) and then immediately converted back to UTF-8. This process effectively cleans the string, replacing any invalid byte sequences with a default character (often '?'). If the input string is already valid UTF-8, the output will be identical.

 

This is useful for sanitizing data received from external sources before sending it in a JSON payload or writing it to a file, preventing errors caused by malformed text.

 

Syntax

 

HTP.FromUTF8|$$INP[|$$OUT]

 

Parameter Explanation

 

P1 - $$INP - (Variable, String, Required)

The input string that is expected to be in UTF-8 format.

 

P2 - $$OUT - (Variable, String, Optional)

The variable to store the validated and normalized UTF-8 string. If omitted, the result is placed on the top-of-stack (TOS).

 

Examples

 

'***********************************

' HTP.FromUTF8 - Sample 1: Validating a string

'***********************************

VAR.$$TXT=Hello, world! With german umlauts: äöüß.

HTP.FromUTF8|$$TXT|$$RES

 

' If the input was valid UTF-8, the output should be identical.

JIV.$$RES!$$TXT|Lab_failed

 

MBX.Success:|The string was valid UTF-8 and remains unchanged.

END.

 

 

Remarks

 

- This command is the primary way to convert text from an external source (like a web request response) into the internal format used by the PSJ library, which operates on Unicode WideSTRINGs.

- If the conversion fails due to a system error, an HTP error will be set. Check with HTP.GetErr.

 

See also:

 

HTP.FixEncoding

HTP.Request