---
schema_version: "1"
id: cbc849d78d70a3f7d6d85c5a4153d03a2cccba6220ec832377f7b2e5771db936
title: toBinary
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dw-coercions-functions-tobinary.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dw-coercions-functions-tobinary
source_site: docs.mulesoft.com
content_type: documentation
content_hash: dcbfc05a90b9b910b5dcc43779c233aba8a0fa8eedcccc0a3289302171879df3
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
---

# toBinary

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## toBinary(str: String, encoding: String): Binary

Transform a `String` value into a `Binary` value using the specified encoding.

_Introduced in DataWeave version 2.4.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>str</code></p></td><td><p>The <code>String</code> value to transform into a <code>Binary</code> value.</p></td></tr><tr><td><p><code>encoding</code></p></td><td><p>The encoding to apply to the <code>String</code> value. Accepts encodings that are supported by your JDK. For example, <code>encoding</code> accepts Java canonical names and aliases for the basic and extended encoding sets in Oracle JDK 8 and JDK 11.</p></td></tr></tbody></table>

### Example

This example shows how `toBinary` behaves with different inputs. It produces output in the
`application/dw` format.

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
  'UTF-16Ex': toBinary("DW", "UTF-16"),
  'utf16Ex': toBinary("DW", "utf16"),
  'UnicodeBigEx': toBinary("DW", "UnicodeBig"),
  'UTF-32Ex': toBinary("DW", "UTF-32"),
  'UTF_32Ex': toBinary("DW", "UTF_32")
}
```

#### Output

```Json
{
  "UTF-16Ex": "/v8ARABX" as Binary {base: "64"},
  utf16Ex: "/v8ARABX" as Binary {base: "64"},
  UnicodeBigEx: "/v8ARABX" as Binary {base: "64"},
  "UTF-32Ex": "AAAARAAAAFc=" as Binary {base: "64"},
  UTF_32Ex: "AAAARAAAAFc=" as Binary {base: "64"}
}
```
