Text.ToBinary
A text as bytes in an encoding, UTF-8 unless you say otherwise.
Group: Text and characters. Power Query M has it too: a query that uses it is written as in Power BI.
Syntax
Text.ToBinary(text as nullable text, optional encoding as nullable number, optional includeByteOrderMark as nullable logical) as nullable binary
| Parameter | Type | Description |
|---|---|---|
text | nullable text | The text to turn into bytes. |
encoding | nullable number optional | The text encoding: TextEncoding.Utf8 (the default), TextEncoding.Utf16 or TextEncoding.Unicode (UTF-16, little end first), TextEncoding.BigEndianUnicode, TextEncoding.Windows (code page 1252), TextEncoding.Ascii, or a code page's number, such as 1250 for Central European. |
includeByteOrderMark | nullable logical optional | true to start the bytes with the encoding's byte order mark. Left out, no mark. |
Returns
nullable binary. The bytes; null when the text is null.
Remarks
- In UTF-8, letters outside plain English take more than one byte: ø is two. In UTF-16 every letter of the Nordic alphabets takes two.
- A letter code page 1252 lacks is written as its nearest plain letter where Windows has one (Ł as L), else as ?.
- Every code page of one byte per character is read and written, and UTF-8, UTF-16 and UTF-32. Of the code pages that take several bytes for a character, such as Japanese Shift-JIS (932), OpenBI handles only the plain ASCII text, and says so when there is more.
Compared with Power Query
Known difference in OpenBI Studio 0.3.79, found while this page was written and run on its engine:
Text.ToBinary("Ā", 1250): Power Query gives #binary({65}): a letter the code page lacks becomes its nearest plain letter, A. OpenBI does that in code page 1252 only, and gives #binary({63}), a question mark, in the others.
Examples
A word in UTF-8
Text.ToBinary("Oslo")
#binary({79, 115, 108, 111})
Two bytes for each ø
Binary.Length(Text.ToBinary("Smørbrød"))
10
Eight letters, ten bytes.
With a byte order mark
Text.ToBinary("Oslo", TextEncoding.Utf8, true)
#binary({239, 187, 191, 79, 115, 108, 111})
UTF-8's mark is the three bytes 239, 187 and 191; some programs need it to read a file as UTF-8.