Charset CkPython Reference Documentation

CkCharset

Current Version: 10.1.2

A component/class for converting character data from one encoding to another. This software is unique in that the data required for converting to/from the supported charsets is entirely embedded witin the Chilkat DLL/library. It does not depend on what code pages may or may not be installed on a particular computer. This allows programs to operate correctly regardless of the locale, OS version, or other factors.

Object Creation

obj = chilkat.CkCharset()

Properties

AltToCharset
# strVal is a string
# ckStr is a CkString
charset.get_AltToCharset(ckStr);
strVal = charset.altToCharset();
charset.put_AltToCharset(strVal);

If the ErrorAction property is set to 6, then this property controls how errors are handled. It specifies an alternate "To" charset. When a character in the input data cannot be converted to the target charset, an attempt is made to convert it to the AltToCharset. If that fails, the input character is dropped.

top
DebugLogFilePath
# strVal is a string
# ckStr is a CkString
charset.get_DebugLogFilePath(ckStr);
strVal = charset.debugLogFilePath();
charset.put_DebugLogFilePath(strVal);

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
ErrorAction
# intVal is an integer
intVal = charset.get_ErrorAction();
charset.put_ErrorAction(intVal);

Controls how errors are handled. When a character in the input data cannot be converted to the target charset, the action taken is controlled by this property. The possible settings are: (0) drop the error characters, (1) substitute the data set by the SetErrorBytes or SetErrorString method, (2) convert to a hex-escaped string (&#xXXXX), (3) RESERVED, (4) RESERVED, (5) RESERVED, (6) convert the error character to the AltToCharset instead, if that fails then drop the character, (7) Pass non-convertible characters to the output unchanged.

top
FromCharset
# strVal is a string
# ckStr is a CkString
charset.get_FromCharset(ckStr);
strVal = charset.fromCharset();
charset.put_FromCharset(strVal);

Tells the charset converter the charset of the input data for a conversion. Possible values are:

<font size="2" face="MS Sans Serif">
us-ascii
unicode  (also known as UTF16LE or simply UTF16)
unicodefffe  (also known as UTF16BE)
ebcdic
iso-8859-1
iso-8859-2
iso-8859-3
iso-8859-4
iso-8859-5
iso-8859-6
iso-8859-7
iso-8859-8
iso-8859-9
iso-8859-13
iso-8859-15
windows-874
windows-1250
windows-1251
windows-1252
windows-1253
windows-1254
windows-1255
windows-1256
windows-1257
windows-1258
utf-7
utf-8
utf-32
utf-32be
shift_jis
gb2312
ks_c_5601-1987
big5
iso-2022-jp
iso-2022-kr
euc-jp
euc-kr
macintosh
x-mac-japanese
x-mac-chinesetrad
x-mac-korean
x-mac-arabic
x-mac-hebrew
x-mac-greek
x-mac-cyrillic
x-mac-chinesesimp
x-mac-romanian
x-mac-ukrainian
x-mac-thai
x-mac-ce
x-mac-icelandic
x-mac-turkish
x-mac-croatian
asmo-708
dos-720
dos-862
ibm01140
ibm01141
ibm01142
ibm01143
ibm01144
ibm01145
ibm01146
ibm01147
ibm01148
ibm01149
ibm037
ibm437
ibm500
ibm737
ibm775
ibm850
ibm852
ibm855
ibm857
ibm00858
ibm860
ibm861
ibm863
ibm864
ibm865
cp866
ibm869
ibm870
cp875
koi8-r
koi8-u
</font>

top
LastErrorHtml
# strVal is a string
# ckStr is a CkString
charset.get_LastErrorHtml(ckStr);
strVal = charset.lastErrorHtml();

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
# strVal is a string
# ckStr is a CkString
charset.get_LastErrorText(ckStr);
strVal = charset.lastErrorText();

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
# strVal is a string
# ckStr is a CkString
charset.get_LastErrorXml(ckStr);
strVal = charset.lastErrorXml();

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastInputAsHex
# strVal is a string
# ckStr is a CkString
charset.get_LastInputAsHex(ckStr);
strVal = charset.lastInputAsHex();

If SaveLast is set to true, then the input and output of a conversion is saved to allow the exact bytes that are sent to the converter to be seen (for debugging purposes). This property shows the last input data in a hexidecimalized string.

top
LastInputAsQP
# strVal is a string
# ckStr is a CkString
charset.get_LastInputAsQP(ckStr);
strVal = charset.lastInputAsQP();

If SaveLast is set to true, then the input and output of a conversion is saved to allow the exact bytes that are sent to the converter to be seen (for debugging purposes). This property shows the last input data in a quoted-printable string.

top
LastMethodSuccess
# boolVal is a boolean
boolVal = charset.get_LastMethodSuccess();
charset.put_LastMethodSuccess(boolVal);

Indicates the success or failure of the most recent method call: True means success, False means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages.

top
LastOutputAsHex
# strVal is a string
# ckStr is a CkString
charset.get_LastOutputAsHex(ckStr);
strVal = charset.lastOutputAsHex();

If SaveLast is set to true, then the input and output of a conversion is saved to allow the exact bytes that are sent to the converter to be seen (for debugging purposes). This property shows the last output data in a hexidecimalized string.

top
LastOutputAsQP
# strVal is a string
# ckStr is a CkString
charset.get_LastOutputAsQP(ckStr);
strVal = charset.lastOutputAsQP();

If SaveLast is set to true, then the input and output of a conversion is saved to allow the exact bytes that are sent to the converter to be seen (for debugging purposes). This property shows the last output data in a quoted-printable string.

top
SaveLast
# boolVal is a boolean
boolVal = charset.get_SaveLast();
charset.put_SaveLast(boolVal);

Tells the component to keep the input/output byte data in memory after a conversion is complete so the data can be examined via the LastInputAsHex/QP and LastOutputAsHex/QP properties. (for debugging purposes)

top
ToCharset
# strVal is a string
# ckStr is a CkString
charset.get_ToCharset(ckStr);
strVal = charset.toCharset();
charset.put_ToCharset(strVal);

Tells the charset converter the target charset for a conversion. Possible values are:

<font size="2" face="MS Sans Serif">
us-ascii
unicode  (also known as UTF16LE or simply UTF16)
unicodefffe  (also known as UTF16BE)
ebcdic
iso-8859-1
iso-8859-2
iso-8859-3
iso-8859-4
iso-8859-5
iso-8859-6
iso-8859-7
iso-8859-8
iso-8859-9
iso-8859-13
iso-8859-15
windows-874
windows-1250
windows-1251
windows-1252
windows-1253
windows-1254
windows-1255
windows-1256
windows-1257
windows-1258
utf-7
utf-8
utf-32
utf-32be
shift_jis
gb2312
ks_c_5601-1987
big5
iso-2022-jp
iso-2022-kr
euc-jp
euc-kr
macintosh
x-mac-japanese
x-mac-chinesetrad
x-mac-korean
x-mac-arabic
x-mac-hebrew
x-mac-greek
x-mac-cyrillic
x-mac-chinesesimp
x-mac-romanian
x-mac-ukrainian
x-mac-thai
x-mac-ce
x-mac-icelandic
x-mac-turkish
x-mac-croatian
asmo-708
dos-720
dos-862
ibm01140
ibm01141
ibm01142
ibm01143
ibm01144
ibm01145
ibm01146
ibm01147
ibm01148
ibm01149
ibm037
ibm437
ibm500
ibm737
ibm775
ibm850
ibm852
ibm855
ibm857
ibm00858
ibm860
ibm861
ibm863
ibm864
ibm865
cp866
ibm869
ibm870
cp875
koi8-r
koi8-u
</font>

top
Utf8
# boolVal is a boolean
boolVal = charset.get_Utf8();
charset.put_Utf8(boolVal);

When set to True, all "const char *" arguments are interpreted as utf-8 strings. If set to False (the default), then "const char *" arguments are interpreted as ANSI strings. Also, when set to True, and Chilkat method returning a "const char *" is returning the utf-8 representation. If set to False, all "const char *" return values are ANSI strings.

top
VerboseLogging
# boolVal is a boolean
boolVal = charset.get_VerboseLogging();
charset.put_VerboseLogging(boolVal);

If set to True, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is False. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
# strVal is a string
# ckStr is a CkString
charset.get_Version(ckStr);
strVal = charset.version();

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

CharsetToCodePage
# charsetName is a string
retInt = charset.CharsetToCodePage(charsetName);

Converts a character set name, such as "iso-8859-1," to its corresponding code page number, like 28591.

top
CodePageToCharset
# codePage is an integer
# outCharset is a CkString (output)
status = charset.CodePageToCharset(codePage, outStr);
retStr = charset.codePageToCharset(codePage);

Converts a code page number to a charset name. For example, 65001 converts to "utf-8".

Returns True for success, False for failure.

top
ConvertData
# inData is a CkByteData
# outData is a CkByteData (output)
status = charset.ConvertData(inData, outData);

Converts character data from one charset to another. Before calling ConvertData, the FromCharset and ToCharset properties must be set to the source and destination charset names, such as "iso-8859-1" or "Shift_JIS".

Returns True for success, False for failure.

top
ConvertFile
# inPath is a string
# destPath is a string
status = charset.ConvertFile(inPath, destPath);

Converts a file from one character encoding to another. The FromCharset and ToCharset properties specify the source and destination character encodings. If the ToCharset is utf-16 or utf-8, then the preamble (also known as BOM) is included in the output. (Call ConvertFileNoPreamble to suppress the output of the BOM.)

Returns True for success, False for failure.

top
ConvertFileNoPreamble
# inPath is a string
# destPath is a string
retBool = charset.ConvertFileNoPreamble(inPath, destPath);

Converts a file from one character encoding to another. The FromCharset and ToCharset properties specify the source and destination character encodings. No preamble (also known as BOM) is included in the output.

top
ConvertFromUnicode
# inData is a string
# outBytes is a CkByteData (output)
status = charset.ConvertFromUnicode(inData, outData);

Converts Unicode (utf-16) text to the charset specified by the ToCharset property.

Returns True for success, False for failure.

top
ConvertFromUtf16
# uniData is a CkByteData
# outMbData is a CkByteData (output)
status = charset.ConvertFromUtf16(uniData, outData);

Converts utf-16 text to the charset specified by the ToCharset property.

Returns True for success, False for failure.

top
ConvertHtml
# inData is a CkByteData
# outHtml is a CkByteData (output)
status = charset.ConvertHtml(inData, outData);

Converts HTML text from one character encoding to another. The FromCharset and ToCharset properties must be set prior to calling this method. This method automatically edits the META tag within the HTML that indicates the charset.

Returns True for success, False for failure.

top
ConvertHtmlFile
# inPath is a string
# destPath is a string
status = charset.ConvertHtmlFile(inPath, destPath);

Converts an HTML file from one character encoding to another. The ToCharset properties must be set prior to calling this method. If the FromCharset is not set, it is obtained from the HTML META tag that indicates the charset. This method automatically edits the META tag within the HTML that indicates the charset.

Returns True for success, False for failure.

top
ConvertToUnicode
# inData is a CkByteData
# outStr is a CkString (output)
status = charset.ConvertToUnicode(inData, outStr);
retStr = charset.convertToUnicode(inData);

Converts multibyte character data to a Unicode string. The FromCharset property should be set before calling this method.

Returns True for success, False for failure.

top
ConvertToUtf16
# mbData is a CkByteData
# outUniData is a CkByteData (output)
status = charset.ConvertToUtf16(mbData, outData);

Converts the mbData to utf-16 bytes.

Returns True for success, False for failure.

top
EntityEncodeDec
# str is a string
# outStr is a CkString (output)
status = charset.EntityEncodeDec(str, outStr);
retStr = charset.entityEncodeDec(str);

Converts non-US-ASCII characters to Unicode decimal entities (&#xxxxx;)

Returns True for success, False for failure.

top
EntityEncodeHex
# str is a string
# outStr is a CkString (output)
status = charset.EntityEncodeHex(str, outStr);
retStr = charset.entityEncodeHex(str);

Converts non-US-ASCII characters to Unicode hex entities (&#xXXXX;)

Returns True for success, False for failure.

top
GetHtmlCharset
# inData is a CkByteData
# outCharset is a CkString (output)
status = charset.GetHtmlCharset(inData, outStr);
retStr = charset.getHtmlCharset(inData);

Examines HTML text and extracts the charset name specified by the META tag, if present.

Returns True for success, False for failure.

top
GetHtmlFileCharset
# htmlFilePath is a string
# outCharset is a CkString (output)
status = charset.GetHtmlFileCharset(htmlFilePath, outStr);
retStr = charset.getHtmlFileCharset(htmlFilePath);

Examines an HTML file and extracts the charset name specified by the META tag, if present.

Returns True for success, False for failure.

top
HtmlDecodeToStr
# inStr is a string
# outStr is a CkString (output)
status = charset.HtmlDecodeToStr(inStr, outStr);
retStr = charset.htmlDecodeToStr(inStr);

Converts HTML entities to Unicode characters.

Returns True for success, False for failure.

top
HtmlEntityDecode
# inHtml is a CkByteData
# outData is a CkByteData (output)
status = charset.HtmlEntityDecode(inHtml, outData);

Decodes HTML entities. See http://www.w3.org/TR/REC-html40/sgml/entities.html for information on HTML entities. Examples of HTML entities are &lt; , &#229; , &#xE5; , &#x6C34; , &Iacute; , etc.

Returns True for success, False for failure.

top
HtmlEntityDecodeFile
# inPath is a string
# destPath is a string
status = charset.HtmlEntityDecodeFile(inPath, destPath);

Decodes HTML entities in a file and creates a new HTML file with the entities decoded. See http://www.w3.org/TR/REC-html40/sgml/entities.html for information on HTML entities. Examples of HTML entities are &lt; , &#229; , &#xE5; , &#x6C34; , &Iacute; , etc.

Returns True for success, False for failure.

top
LowerCase
# inStr is a string
# outStr is a CkString (output)
status = charset.LowerCase(inStr, outStr);
retStr = charset.lowerCase(inStr);

Converts a string to lowercase.

Returns True for success, False for failure.

top
ReadFile
# path is a string
# outData is a CkByteData (output)
status = charset.ReadFile(path, outData);

Convenience method for reading the entire contents of a file into a byte array.

Returns True for success, False for failure.

top
ReadFileToString
# path is a string
# charset is a string
# outStr is a CkString (output)
status = charset.ReadFileToString(path, charset, outStr);
retStr = charset.readFileToString(path, charset);

Reads a text file and returns the text converted to a Unicode string. The filename is specified by the first method argument, and the charset of the text data is specified by the 2nd method argument.

Returns True for success, False for failure.

top
SetErrorString
# str is a string
# charset is a string
charset.SetErrorString(str, charset);

If the ErrorAction property is set to 1, the string passed to this method is used as the result for any characters that cannot be converted during a conversion.

top
UpperCase
# inStr is a string
# outStr is a CkString (output)
status = charset.UpperCase(inStr, outStr);
retStr = charset.upperCase(inStr);

Converts a string to uppercase.

Returns True for success, False for failure.

top
UrlDecodeStr
# inStr is a string
# outStr is a CkString (output)
status = charset.UrlDecodeStr(inStr, outStr);
retStr = charset.urlDecodeStr(inStr);

URL decodes a string.

This method assumes the ANSI byte representation was used for encoding. For example, the letter É, where the ANSI charset is Latin-1, would appear as "%C9" when URL encoded. (As opposed to the utf-8 encoding where É is represented by 2 bytes 0xC3, 0x89 and would appear as "%C3%89" when URl encoded. This method assumes ANSI encoding.

You can use encoding/decoding methods in Chilkat's StringBuilder class to specify the charset from which to decode or encode.

Returns True for success, False for failure.

top
VerifyData
# charset is a string
# inData is a CkByteData
retBool = charset.VerifyData(charset, inData);

Returns true if the byte data conforms to the charset passed in the first argument.

top
VerifyFile
# charset is a string
# path is a string
retBool = charset.VerifyFile(charset, path);

Returns true if the file contains character data that conforms to the charset passed in the 1st argument.

top
WriteFile
# path is a string
# byteData is a CkByteData
retBool = charset.WriteFile(path, byteData);

Convenience method for saving an entire byte array to a file.

top
WriteStringToFile
# textData is a string
# path is a string
# charset is a string
status = charset.WriteStringToFile(textData, path, charset);

Converts a Unicode string to a multibyte charset and writes the multibyte text data to a file. The destination charset is specified in the 2nd method argument.

Returns True for success, False for failure.

top