BinaryFormat.Text

D

T

The M Code Behind the Power Query M function BinaryFormat.Text

What is BinaryFormat.Text?

BinaryFormat.Text is an M function that converts binary data into a string of text. This can be useful when working with data that is stored in a binary format, such as images, PDFs, or other files. By converting the binary data into text, you can more easily read and manipulate the data in your Power Query transformations.

How to Use BinaryFormat.Text

To use BinaryFormat.Text, you first need to understand the parameters that the function accepts. The function has two parameters: the binary data that you want to convert, and the encoding type that you want to use for the conversion.

The binary data can be any binary data source that you have loaded into your Power Query project. This can include data from a file, a web page, or a database. The encoding type determines how the binary data is converted into text. Common encoding types include ASCII, Unicode, and UTF-8.

Once you have the binary data and encoding type, you can use the BinaryFormat.Text function to convert the data into text. Here is an example of how to use the function:


let

binaryData = File.Contents(“C:MyFile.pdf”),

textData = BinaryFormat.Text(binaryData, Encoding.Ascii)

in

textData


In this example, we are loading a PDF file (stored as binary data) and converting it into text using the ASCII encoding type. The resulting text can then be used for further Power Query transformations.

Understanding the M Code

Now that we understand how to use BinaryFormat.Text, let's take a closer look at the M code behind the function. The function is defined as follows:


BinaryFormat.Text(binary as binary, optional encoding as nullable number) as text


The first parameter, `binary`, is the binary data that we want to convert. This parameter is required and must be of type binary.

The second parameter, `encoding`, is optional and determines the encoding type that we want to use for the conversion. If this parameter is not provided, the function will default to using the system's default encoding type. This parameter is of type nullable number, which means that it can be either a number or null.

The function then returns the converted text as a string.

Encoding Types

As mentioned earlier, encoding types determine how binary data is converted into text. There are several different encoding types that you can use with BinaryFormat.Text, each with its own advantages and disadvantages.

ASCII

ASCII is a widely used encoding type that represents characters using 7 bits. This means that it can represent a total of 128 different characters, which includes the alphabet, numbers, and some special characters. ASCII is a good choice if you are working with simple, English-only text data.

Unicode

Unicode is a more advanced encoding type that can represent characters from many different languages and scripts. It uses 16 bits to represent each character, which means it can represent a total of 65,536 different characters. Unicode is a good choice if you are working with data that includes characters from non-English languages.

UTF-8

UTF-8 is a variable-length encoding type that can represent characters from many different languages and scripts. It uses between 1 and 4 bytes to represent each character, depending on the character's Unicode value. UTF-8 is a good choice if you are working with data that includes a mix of English and non-English characters.

BinaryFormat.Text is a powerful M function that can help you work with binary data in your Power Query projects. By understanding the M code behind the function and the different encoding types available, you can more effectively convert binary data into usable text. Whether you are working with images, PDFs, or other binary data sources, BinaryFormat.Text can help you unlock the full potential of your data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)