BinaryFormat.UnsignedInteger64

D

T

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

What is BinaryFormat.UnsignedInteger64?

BinaryFormat.UnsignedInteger64 is a Power Query M function that takes a number as its input and returns an unsigned 64-bit binary string representation of that number. This function is particularly useful for working with binary data, such as when dealing with binary file formats or when performing bitwise operations.

The function takes two optional arguments: the first argument specifies the number of bits to use (default is 64), while the second argument specifies whether to use little-endian or big-endian byte order (default is false, meaning big-endian).

Here is an example of how to use the BinaryFormat.UnsignedInteger64 function:


BinaryFormat.UnsignedInteger64(1234567890)


This will return the binary string "00000000 00000000 00000000 00000000 01001010 01100110 10000001 00110010".

The M code behind BinaryFormat.UnsignedInteger64

The M code behind BinaryFormat.UnsignedInteger64 is actually quite simple. Here is the complete function definition:


(BinaryFormat.UnsignedInteger64) =>

let

binary = Number.ToText(_, “###############0”),

padded = Text.PadStart(binary, 64, “0”),

grouped = List.Transform({0..7}, each Text.Combine(List.Reverse(List.Range(Text.ToList(padded), _ 8, 8)), “”)),

result = Text.Combine(grouped, ” “)

in

result


Let's break this down step by step:

1. The function takes a number as its input, which is represented by the underscore (_) in the function definition.

2. The Number.ToText function is used to convert the input number to a string of digits with no decimal places. The "###############0" format specifier ensures that the string is at least 15 characters long and includes a leading zero if necessary.

3. The Text.PadStart function is used to pad the string with zeros on the left until it is 64 characters long.

4. The List.Transform function is used to split the padded string into 8-byte chunks, and then reverse the order of the characters in each chunk.

5. The Text.Combine function is used to combine the reversed chunks into a single string, with a space between each chunk.

And that's it! The resulting string is the binary representation of the input number, using big-endian byte order.

BinaryFormat.UnsignedInteger64 is a useful function in Power Query M that allows you to easily convert a number into an unsigned 64-bit binary string. The M code behind this function is simple and easy to understand, making it a great example of how Power Query M functions are built. With this knowledge, you can now confidently use BinaryFormat.UnsignedInteger64 in your own Power Query projects.

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)