Method

# ConvertEncoding

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

## Description

Provides a quick way to convert a string of known encoding to some other encoding, without having to create a `TextConverter</api/text/encoding_text/textconverter>` object.

## Usage

``` xojo
result = sourceVariable.ConvertEncoding(newEncoding)
```

| Part           | Type                                                 | Description                                                                                                                                    |
|----------------|------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| result         | `String</api/data_types/string>`                     | The converted string using the *newEncoding* TextEncoding.                                                                                     |
| sourceVariable | `String</api/data_types/string>`                     | The string to be converted. It must already have an encoding. Use `DefineEncoding</api/text/encoding_text/defineencoding>` to set an encoding. |
| newEncoding    | `TextEncoding</api/text/encoding_text/textencoding>` | The encoding to be used in the conversion.                                                                                                     |

## Notes

When you need to write text to a file that will be opened by another app that expects a particular encoding, use <span class="title-ref">ConvertEncoding</span> to convert the text to that encoding before you call the Write method.

The string must already have an encoding in order to convert it to something else. Use `DefineEncoding</api/text/encoding_text/defineencoding>` to set an encoding if it does not have one.

## Sample code

The following code use the `Encodings</api/text/encoding_text/encodings>` module to convert the text in a `TextField</api/user_interface/desktop/desktoptextfield>` to the ANSI encoding:

``` xojo
Var result As String
result = TextField1.Text.ConvertEncoding(Encodings.WindowsANSI)
```

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

Here is an example that converts the text in a `TextField</api/user_interface/desktop/desktoptextfield>` to the MacRoman encoding.

``` xojo
Var f As FolderItem
Var fileStream As TextOutputStream

file = FolderItem.SaveFileDialog(FileTypes1.Text, "My Info.txt")

If f <> Nil Then
  fileStream = TextOutputStream.Create(f)
  fileStream.Write(nameField.Text.ConvertEncoding(Encodings.MacRoman))
  fileStream.Close
End If
```

## Compatibility

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

<div class="seealso">

`TextConverter</api/text/encoding_text/textconverter>`, `TextEncoding</api/text/encoding_text/textencoding>`, `TextOutputStream</api/files/textoutputstream>` classes; `DefineEncoding</api/text/encoding_text/defineencoding>`, `Encoding</api/text/encoding_text/encoding>` functions; `Encodings</api/text/encoding_text/encodings>` module.

</div>
