From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Returns a String with the same data as the given string, but with the encoding of the passed encoding.


result = sourceVariable.DefineEncoding(encoding)

Part Type Description
result String The result of encoding str using the encoding specified by enc.
sourceVariable String The String to be encoded.
encoding TextEncoding The TextEncoding to be used to encode str.


This function is useful when you have a string whose encoding is known to you but not to your application. The encoding of all strings created in your application is UTF-8, so you don't have to use DefineEncoding on them.

Consult the values of Base entry for TextEncoding when creating the TextEncoding object using the GetTextEncoding function.

Sample Code

The following code takes 8 bytes from a MemoryBlock and sets the encoding to UTF16.

Var source As String = MyMemoryBlock.StringValue(0, 8)
TextField1.Text = source.DefineEncoding(Encodings.UTF16)

This code uses DefineEncoding when reading text from a TCPSocket.

TextField1.Text = TCPSocket1.ReadAll.DefineEncoding(Encodings.UTF8)

See Also

TextEncoding class; String.ConvertEncoding, Encoding, GetTextEncoding functions; Encodings module.