[Overview][Constants][Types][Procedures and functions][Variables][Index] Reference for unit 'LConvEncoding' (#lazutils)

ConvertEncoding

Converts the specified value from its source encoding to a target encoding using an optional code page.

Declaration

Source position: lconvencoding.pas line 114

function ConvertEncoding(

  const s: string;

  const FromEncoding: string;

  const ToEncoding: string;

  SetTargetCodePage: Boolean = False

):string;

Arguments

s

  

String value converted in the routine.

FromEncoding

  

Encoding name for the original string value.

ToEncoding

  

Encoding name for the converted value.

SetTargetCodePage

  

True if the code page should be set for the converted value. Default is False.

Function result

Value for the specified string after encoding conversion.

Description

ConvertEncoding is a String function used to convert the string specified in s from its original encoding name (FromEncoding) to a target encoding (ToEncoding).

When FromEncoding is omitted, or specified as an empty string (''), UTF-8 is used as the original encoding. If the original encoding is ANSI, the default text encoding for the platform (GetDefaultTextEncoding) is used as the original encoding.

Conversely, when ToEncoding is omitted or an empty string, UTF-8 is used as the target encoding. If the target encoding is ANSI, the default text encoding for the platform (GetDefaultTextEncoding) is used as the target encoding.

No actions are performed in the routine when FromEncoding and ToEncoding have the same values.

ConvertEncoding calls ConvertEncodingFromUTF8 to convert the string value when the FromEncoding is UTF-8. ConvertEncodingToUTF8 is called when ToEncoding is UTF-8. For other encoding conversions, ConvertEncodingToUTF8 is called to both get a UTF-8 value which is then converted using ConvertEncodingFromUTF8.

SetTargetCodePage indicates whether the string with the converted encoding should set the code page for the result. The default value for the argument is False. The SetTargetCodePage argument is ignored when ToEncoding is the UTF-8 encoding ('utf8'). The code page is always set to CP_UTF8 (65001) when UTF-8 encoding is used in the result.

For platforms where libiconv is enabled, the iconvert routine is used as a fallback when an encoding conversion routine was not found.

The return value is the original string in s if a transcoding operation is not successfully performed (or not needed) for the original string value using the specified encodings.


Version 3.2 Generated 2024-02-25 Home