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

# 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"}
}
```
