Hi,
I am having a formula which currently runs on Button.OnSelect without problems. The formula converts a hex value of any length to its decimal equivalence.
This formula has to be converted because it should run now in a Label.Text property.
The problem I'm having is to replace the Collect and CountRows function. I tried a lot of different things but I always struggle with the CountRows() formula that is needed to set the record number of a table (which is needed for calculation).
It would be very nice if you could throw an eye on it.
Thanks in advance
Thomas
Steps to run the current code:
- Create a Label (hexString.Text = "#4A1A")
- Create a Label (Label.Text = _res)
- Create a Button (Button.OnSelect = <copy the code from below>)
// convert a hex value string of any length to its decimal equivalence
// testing with the following hexString.Text properties:
// '#4A1A', '#4a1a', '4A1A' or '4a1a'
// expected result:
// 18970
Clear(coll1);
ForAll(
// split the hex string into a single column table ["4", "A", "1", "A"]
// or ["4", "a", "1", "a"]
// the table will contain 4 records
Split(
If(Left(hexString.Text, 1) <> "#", hexString.Text,
Mid(hexString.Text, 2, Len(hexString.Text) -1)
),
""
),
// fill collection 'coll1' based on split table ["4", "A", "1", "A"]
// calculation per record:
// Find(Upper(Result)...) -1 returns a value between 0 and 15
// (the decimal equivalent for the given hex char)
// "4" => 4
// "A" => 10
// "1" => 1
// "A" => 10
// multiply this by
// Power(16, (length of the hex string (minus 1 if starts with '#')) - 1
// - (current number of records in collection (starts with 0)) -1
// Power(16, 3 - 0) => Power(16, 3) => 4096
// Power(16, 3 - 1) => Power(16, 2) => 256
// Power(16, 3 - 2) => Power(16, 1) => 16
// Power(16, 3 - 3) => Power(16, 0) => 1
// records:
// {decValue: 4 * 4096} => 16384
// {decValue: 10 * 256} => 2560
// {decValue: 1 * 16} => 16
// {decValue: 10 * 1} => 10
Collect(coll1,
{
decValue: (
Find(Upper(Result), "0123456789ABCDEF") -1)
* Power(16,
Len(hexString.Text) -1 - If(Left(hexString.Text, 1) = "#", 1, 0)
- CountRows(coll1)
)
}
)
);
// _res = 18970 (16384 + 2560 + 16 + 10)
// _res is set as Label.Text property to display the result
Set(_res, Sum(coll1, decValue))