Method

# EncodeBase64

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

## Description

Converts a `String</api/data_types/string>` to Base64 format.

## Usage

``` xojo
result = EncodeBase64(str, lineWrap)
```

| Part     | Type                               | Description                                                                       |
|----------|------------------------------------|-----------------------------------------------------------------------------------|
| *result* | `String</api/data_types/string>`   | The Base64 representation of *str*.                                               |
| *str*    | `String</api/data_types/string>`   | The `String</api/data_types/string>` expression to be converted to Base64 format. |
| lineWrap | `Integer</api/data_types/integer>` | Optional: Specifies the maximum number of characters per line.                    |

## Notes

Base64 is used for email file attachments. SMTP was designed around the ASCII encoding, so it assumes that characters below ASCII 32 are control codes and it doesn't expect codes above ASCII 127. <span class="title-ref">EncodeBase64</span> lets you encode arbitrary binary data into a 64-character alphabet composed of the printable ASCII characters. Three bytes of input become four characters of output. <span class="title-ref">EncodeBase64</span> is also used in authentication schemes as a way to send an encryption key or hash value through a link that expects text.

The `DecodeBase64</api/text/encoding_text/decodebase64>` function performs the reverse operation, taking a Base64 expression and converting it to the original data.

If you tell <span class="title-ref">EncodeBase64</span> to line wrap and want a different line ending than CRLF, you'll need to use `String.ReplaceLineEndings<string.replacelineendings>` as appropriate.

<div class="warning">

<div class="title">

Warning

</div>

On Android, any value other than 76 is treated as 0 (no line wrap).

</div>

## Sample code

The following code encodes UTF-8 text for transmission without line breaks:

``` xojo
Var myText As String = "Hello, world!"
Var encodedText As String = EncodeBase64(myText, 0)
' encodedText is "SGVsbG8sIHdvcmxkIQ=="
```

## Compatibility

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

<div class="seealso">

- `DecodeBase64</api/text/encoding_text/decodebase64>` function.
- [Base64 encoding on Wikipedia](https://en.wikipedia.org/wiki/Base64)

</div>
