Forum Discussion
Number to Binary
- 9 years ago
I addition to my previous respons.
Some of the things I realized recently, is that:
an "if ... then ... else" statement with big chunks of code like above, is not required.
Thanks to concept of "Lazy Evaluation" in Power Query, you can just put in the code without if...then...else.
Intead, the choice is made later. In the example below: in the Result step.
So the inner part of the previous function (which is the actual function without documentation), can be rewritten as:
fnNBC = (input as anynonnull, base as number, optional outputlength as number) as any => let // input = 10, // base = 2, // outputlength = null, Base16 = "0123456789ABCDEF", Base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", Lookups = List.Zip({{16,32,64},{Base16,Base32,Base64}}), Lookup = Text.ToList(List.Last(List.Select(Lookups,each _{0} <= List.Max({16, base}))){1}), InputToList = Text.ToList(input), // This part will be executed if input is text: Reversed = List.Reverse(InputToList), BaseValues = List.Transform(Reversed, each List.PositionOf(Lookup,_)), Indexed = List.Zip({BaseValues, {0..Text.Length(input)-1}}), Powered = List.Transform(Indexed, each _{0}*Number.Power(base,_{1})), Decimal = List.Sum(Powered), // So far this part // This part will be executed if input is not text: Elements = 1+Number.RoundDown(Number.Log(input,base),0), Powers = List.Transform(List.Reverse({0..Elements - 1}), each Number.Power(base,_)), ResultString = List.Accumulate(Powers, [Remainder = input,String = ""], (c,p) => [Remainder = c[Remainder] - p * Number.RoundDown(c[Remainder] / p,0), String = c[String] & Lookup{Number.RoundDown(c[Remainder]/p,0)}])[String], PaddedResultString = if outputlength = null then ResultString else Text.PadStart(ResultString,outputlength,Lookup{0}), // So far this part Result = if input is text then Decimal else PaddedResultString in Result
I know it is an old topic, but I just tried to reuse this code snippet, and it does not work for 0 and "0". Has anybody made that correction?
It should work with "0", but not if the base is 32, which has no "0" in the 32 characters.
A small correction to have it work with 0:
in step "Elements" I added a List.Max formula, in order to supply a minimum of 1 as argument to Number.Log.
fnNBC = (input as anynonnull, base as number, optional outputlength as number) as any =>
let
// input = 10,
// base = 2,
// outputlength = null,
Base16 = "0123456789ABCDEF",
Base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
Lookups = List.Zip({{16,32,64},{Base16,Base32,Base64}}),
Lookup = Text.ToList(List.Last(List.Select(Lookups,each _{0} <= List.Max({16, base}))){1}),
InputToList = Text.ToList(input),
// This part will be executed if input is text:
Reversed = List.Reverse(InputToList),
BaseValues = List.Transform(Reversed, each List.PositionOf(Lookup,_)),
Indexed = List.Zip({BaseValues, {0..Text.Length(input)-1}}),
Powered = List.Transform(Indexed, each _{0}*Number.Power(base,_{1})),
Decimal = List.Sum(Powered),
// So far this part
// This part will be executed if input is not text:
Elements = 1+Number.RoundDown(Number.Log(List.Max({1,input}),base),0),
Powers = List.Transform(List.Reverse({0..Elements - 1}), each Number.Power(base,_)),
ResultString = List.Accumulate(Powers,
[Remainder = input,String = ""],
(c,p) => [Remainder = c[Remainder] - p * Number.RoundDown(c[Remainder] / p,0),
String = c[String] & Lookup{Number.RoundDown(c[Remainder]/p,0)}])[String],
PaddedResultString = if outputlength = null then ResultString else Text.PadStart(ResultString,outputlength,Lookup{0}),
// So far this part
Result = if input is text then Decimal else PaddedResultString
in
Result