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

What is the BinaryFormat.UnsignedInteger16 function?

The BinaryFormat.UnsignedInteger16 function is part of the Power Query M language. It is used to convert binary data into a 16-bit unsigned integer value. This function takes a binary value as input and returns a 16-bit integer value. The function is useful for working with binary data that represents numbers, such as in network protocols or file formats.

How does the BinaryFormat.UnsignedInteger16 function work?

The BinaryFormat.UnsignedInteger16 function works by taking a binary value as input and converting it into a 16-bit unsigned integer value. The function uses the little-endian byte order, which means that the least significant byte is stored first in memory.

To understand how the function works, let’s take a closer look at the M code behind it. The M code for the BinaryFormat.UnsignedInteger16 function is as follows:

BinaryFormat.UnsignedInteger16(binary as binary) as number =>


value = BinaryFormat.Record(ByteOrder.LittleEndian, {{“value”, BinaryFormat.UnsignedInteger16}}, binary),

result = value[value]



The function takes a binary value as input, which is stored in the binary parameter. The function then uses the BinaryFormat.Record function to create a record that contains the binary data. The record is created with the ByteOrder.LittleEndian option, which specifies that the least significant byte is stored first. The record contains a single field called "value", which is of type BinaryFormat.UnsignedInteger16.

The BinaryFormat.UnsignedInteger16 function is then called on the "value" field of the record to convert the binary data into a 16-bit unsigned integer value. The result of the function is stored in the result variable and returned as the output of the function.

Examples of using the BinaryFormat.UnsignedInteger16 function

Let's take a look at some examples of using the BinaryFormat.UnsignedInteger16 function.

Example 1: Converting a binary value into a 16-bit unsigned integer

Suppose we have a binary value that represents the number 12345. The binary value is as follows:

00000000 00110000 00111001 00111001

To convert this binary value into a 16-bit unsigned integer, we can use the BinaryFormat.UnsignedInteger16 function as follows:


binary = Binary.FromText(“00000000001100000011100100111001”, BinaryEncoding.Base2),

value = BinaryFormat.UnsignedInteger16(binary)



The output of this function is the number 12345, which is the value represented by the binary data.

Example 2: Working with binary data in a file format

Suppose we have a file that contains binary data in a specific format. The format of the file is as follows:

offset (2 bytes) | data (30 bytes)

The offset field is a 16-bit unsigned integer that represents the offset of the data in the file. The data field is a 30-byte block of data.

To read the data from the file, we can use the following M code:


file = File.Contents(“C:data.bin”),

binary = Binary.Buffer(file),

offset = BinaryFormat.UnsignedInteger16(binary),

data = Binary.Skip(binary, offset)



The File.Contents function is used to read the contents of the file into a binary value. The Binary.Buffer function is used to create a buffer of the binary data, which improves performance when working with large binary data.

The BinaryFormat.UnsignedInteger16 function is used to read the offset value from the binary data. The Binary.Skip function is then used to skip the offset bytes in the binary data and read the data field.

The BinaryFormat.UnsignedInteger16 function is a powerful tool for working with binary data in Power Query. It allows you to convert binary data into a 16-bit unsigned integer value, which is useful for working with network protocols, file formats, and other binary data formats. By understanding the M code behind this function, you can better understand how it works and how to use it in your own projects.

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

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)