Method

# DefineEncoding

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Returns a `String</api/data_types/string>` with the same data as the given string, but with the encoding of the passed encoding.

## Usage

``` xojo
result = sourceVariable.DefineEncoding(encoding)
```

| Part           | Type                                                 | Description                                                                          |
|----------------|------------------------------------------------------|--------------------------------------------------------------------------------------|
| result         | `String</api/data_types/string>`                     | The result of encoding *str* using the encoding specified by *encoding*.             |
| sourceVariable | `String</api/data_types/string>`                     | The `String</api/data_types/string>` to be encoded.                                  |
| encoding       | `TextEncoding</api/text/encoding_text/textencoding>` | The `TextEncoding</api/text/encoding_text/textencoding>` to be used to encode *str*. |

## Notes

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 <span class="title-ref">DefineEncoding</span> on them.

Consult the values of *Base* entry for `TextEncoding</api/text/encoding_text/textencoding>` when creating the `TextEncoding</api/text/encoding_text/textencoding>` object using the `GetTextEncoding</api/text/encoding_text/gettextencoding>` function.

## Sample code

The following code takes 8 bytes from a `MemoryBlock</api/language/memoryblock>` and sets the encoding to UTF16.

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This code uses <span class="title-ref">DefineEncoding</span> when reading text from a `TCPSocket</api/networking/tcpsocket>`.

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`TextEncoding</api/text/encoding_text/textencoding>` class; `String</api/data_types/string>`, `Encoding</api/text/encoding_text/encoding>`, `GetTextEncoding</api/text/encoding_text/gettextencoding>` functions; `Encodings</api/text/encoding_text/encoding>` module.

</div>
