This module contains helper functions for working with binaries.

To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Binaries to the header of your DataWeave script.

Functions

fromBase64

fromBase64(String): Binary

Transforms a Base64 string into a binary value.

Parameters
Name Description

base64String

The Base64 string to transform.

Example

This example takes a Base64 string and transforms it into a binary.

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/json
---
{ "BinaryFromBase64" : fromBase64("TXVsZQ==") }
Output
1
{ "BinaryFromBase64": "Mule" }

fromHex

fromHex(String): Binary

Transforms a hexadecimal string into a binary.

Parameters
Name Description

hexString

A hexadecimal string to transform.

Example

This example transforms a hexadecimal string to "Mule".

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/json
---
{ "hexToBinary": fromHex("4D756C65") }
Output
1
{ "hexToBinary": "Mule" }

readLinesWith

readLinesWith(Binary, String): Array<String>

Splits the specified Binary content into lines and returns the results in an array.

Parameters
Name Description

content

Binary data to read and split.

charset

String representing the encoding to read.

Example

This example transforms binary content, which is separated into new lines (\n), into a comma-separated array.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::core::Binaries
var content = read("Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n", "application/octet-stream")
output application/json
---
{
   lines : (content readLinesWith "UTF-8"),
   showType: typeOf(content)
}
Output
1
2
3
4
{
   "lines": [ "Line 1", "Line 2", "Line 3", "Line 4", "Line 5" ],
   "showType": "Binary"
}

toBase64

toBase64(Binary): String

Transforms a binary value into a Base64 string.

Parameters
Name Description

content

The binary value to transform.

Example

This example transforms a binary to Base64.

Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Binaries
var myBinary = "Mule" as Binary
output application/json
---
{ "BinaryToBase64" : toBase64(myBinary) }
Output
1
 { "BinaryToBase64": "TXVsZQ==" }

toHex

toHex(Binary): String

Transforms a binary value into a hexadecimal string.

Parameters
Name Description

content

The Binary value to transform.

Example

This example transforms a binary version of "Mule" (defined in the variable, myBinary) to hexadecimal.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::core::Binaries
output application/json
var myBinary = "Mule" as Binary
var testType = typeOf(myBinary)
---
{
   "binaryToHex" : toHex(myBinary)
}
Output
1
{ "binaryToHex": "4D756C65" }

writeLinesWith

writeLinesWith(Array<String>, String): Binary

Writes the specified lines and returns the Binary content.

Parameters
Name Description

content

Array of items to write.

charset

String representing the encoding to use when writing.

Example

This example inserts a new line (\n) after each iteration. Specifically, it uses map to iterate over the result of to(1, 10), [1,2,3,4,5], then writes the specified content ("Line $"), which includes the unnamed variable $ for each number in the array.

Note that without writeLinesWith "UTF-8", the expression { lines: to(1, 10) map "Line $" } simply returns an array of line numbers as the value of an object: { "lines": [ "line 1", "line 2", "line 3", "line 4", "line 5" ] }.

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/json
---
{ lines: to(1, 10) map "Line $" writeLinesWith  "UTF-8" }
Output
1
2
3
{
  "lines": "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n"
}