Lines.ToBinary
LinesConverts a list of text lines to a binary value using the specified encoding and line separator options.
Syntax
Lines.ToBinary(lines as list, optional quoteStyle as nullable number, optional includeByteOrderMark as nullable logical, optional encoding as nullable number) as binaryParameters
| Name | Type | Required | Description |
|---|---|---|---|
lines | list | Yes | A list of text values, each representing one line. |
quoteStyle | number | No | Controls how line endings within quoted fields are handled. Use QuoteStyle.None (default) or QuoteStyle.Csv. |
includeByteOrderMark | logical | No | When true, prepends a byte order mark (BOM) to the output. Defaults to false. |
encoding | number | No | A TextEncoding value specifying the character encoding. Defaults to TextEncoding.Utf8 (65001). |
Return Value
binary — A binary value containing the lines joined with line separators and encoded using the specified encoding.
Remarks
Lines.ToBinary converts a list of text lines into a binary value by joining the lines with a line feed separator (#(lf)) and encoding the result using the specified character encoding. It is the binary-output counterpart to Lines.ToText, and the inverse of reading a binary file and splitting it into lines.
This function is primarily used when you need to produce binary file content from structured line data — for example, writing a CSV or log file to a data store, passing line-based content to a web API, or buffering the result with Binary.Buffer for repeated use. The encoding parameter defaults to TextEncoding.Utf8 (65001), which is appropriate for the vast majority of modern use cases.
Set includeByteOrderMark to true when the consuming system requires a UTF-8 BOM. This is most commonly needed for CSV files opened in Excel, which uses the BOM to detect UTF-8 encoding and correctly render non-ASCII characters. Without the BOM, Excel may misinterpret the encoding and display garbled text.
When you only need text output (not binary), use Lines.ToText instead — it is simpler and does not require specifying encoding parameters.
Examples
Example 1: Convert a list of lines to UTF-8 binary
Lines.ToBinary({"CustomerName,Product", "Alice,Widget A", "Bob,Gadget B"})Result | |
|---|---|
| 1 | Q3VzdG9tZXJOYW1lLFByb2R1Y3QKQWxpY2UsV2lkZ2V0IEEKQm9iLEdhZGdldCBC |
Example 2: Include a UTF-8 byte order mark for Excel compatibility
Lines.ToBinary({"Name,Salary", "Alice,55000", "Bob,95000"}, null, true, TextEncoding.Utf8)Result | |
|---|---|
| 1 | 77u/TmFtZSxTYWxhcnkKQWxpY2UsNTUwMDAKQm9iLDk1MDAw |
Example 3: Round-trip lines through binary and back
let
original = {"Alice,Widget A", "Bob,Gadget B", "Charlie,Widget C"},
asBinary = Lines.ToBinary(original),
asText = Text.FromBinary(asBinary),
backToLines = Lines.FromText(asText)
in
backToLinesThe final output — splits the multi-line text back into the original list of three lines.
Result | |
|---|---|
| 1 | Alice,Widget A,Bob,Gadget B,Charlie,Widget C |