BinaryFormat.SignedInteger32

BinaryFormat.SignedInteger32

D

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

The M code behind the BinaryFormat.SignedInteger32 function is relatively simple. It takes a binary value as input and returns an integer value with a length of 32 bits. Here’s the M code for the BinaryFormat.SignedInteger32 function:


let

BinaryToInt32 = (binary) =>

let

binaryAsList = Binary.ToList(binary),

paddedBinary = List.Combine({List.Repeat({0}, 32 – List.Count(binaryAsList)), binaryAsList}),

sign = Number.FromBinary(List.First(paddedBinary)),

magnitude = List.Accumulate(

List.Skip(paddedBinary, 1),

0,

(state, current) => state 2 + Number.FromBinary(current)

)

in

if sign = 0 then magnitude else magnitude – 2147483648

in

BinaryToInt32


Let's break down this code for a better understanding:

- The BinaryFormat.SignedInteger32 function takes one parameter, which is the binary data to be converted to an integer.

- The Binary.ToList function converts the binary data into a list of 1's and 0's.

- The List.Repeat function adds padding to the binary data so that it has a total of 32 bits.

- The List.Combine function combines the original binary data and the padded bits into a single list.

- The Number.FromBinary function converts the first bit of the binary data to a number, which represents the sign of the integer.

- The List.Skip function removes the first bit of the binary data, leaving only the magnitude of the integer.

- The List.Accumulate function combines the remaining bits of the binary data into a single integer value.

- Finally, the function returns the signed 32-bit integer value, with a magnitude of the binary data and the sign determined by the first bit.

In conclusion, the M code behind the BinaryFormat.SignedInteger32 function is a straightforward and efficient way to convert binary data into signed 32-bit integers. This function is an excellent tool for data analysts who work with binary data regularly, and it can save a lot of time and effort.

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)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: