
前一段时间我写了一篇关于如何使用的博客文章 Power 双 中的Unicode字符. In 那 blogpost I used a recursive 功率查询 function to convert 十六进制 价值观 to Dec 价值观. A few weeks back one of my site visitors kindly shared 他的Power Query函数的非递归版本 做得很好。大声喊着 罗科·卢波(Rocco Lupoi) 共享他的代码。因此,我决定与所有人共享它,以便更多的人可以利用他出色的Power Query功能。虽然我已经触及了他的代码,但这只是表面上的变化,因此本博文的所有功劳归Rocco所有。他的代码的好处不仅限于非递归。 的 code below converts numbers of any base when the base is smaller than 16 like Binary 和 Oct, so it is 不 仅限于 十六进制 价值观 只要. 的 other benefit of the below code is 那 it is 不 case sensitive (note to the digits
踩下面的代码)。
这里是 fnHex2Dec
电源查询功能:
(input as text, optional base as number) as number =>
let
价值观 = [
0=0,
1=1,
2=2,
3=3,
4=4,
5=5,
6=6,
7=7,
8=8,
9=9,
A=10,
B=11,
C=12,
D=13,
E=14,
F=15
],
digits = Text.ToList(Text.Upper(input)),
dim = List.Count(digits)-1,
exp = if base=null then 16 else base,
Result = List.Sum(
List.Transform(
{0..dim}
, each Record.Field(values, digits{_}) * Number.Power(exp, dim - _)
)
)
in
Result
As you see in the code above, the base
parameter is optional, so if 不 provided base 16 would be the default.
这就是我们可以调用上面的函数的方式:
fnHex2Dec("AbCdEf", null)
