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.


Name Description


The String value to transform into a Binary value.


The encoding to apply to the String value. Accepts encodings that are supported by your JDK. For example, encoding accepts Java canonical names and aliases for the basic and extended encoding sets in Oracle JDK 8 and JDK 11.


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


%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")


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

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Submit your feedback!
Share your thoughts to help us build the best documentation experience for you!
Take our latest survey!