Convert Binary to Decimal

Hi guys,

I need to convert binary data to decimal data. But I couldn’t find any function nor module on Make to execute that conversion. So I ended building a small scenario to perform that conversion.

The thing is, as you can see on the screenshot below, the scenario uses up more than 120 operations just to execute the binary-to-decimal conversion of 1 data point. Since I’ll have to convert many data points, this is too operation-consuming to me.

Do you guys have a more elegant solution to suggest?

Hi @JBJ,

Just to be clear, do you have an example of input and expected output?

Hi @JBJ,

I just saw your other post and I think I have an idea, but not very elegant one. What about you use Google Sheet and write your binary in a cell and use Sheet formula to convert it to binary? It’ll cost 2 operations… update a cell and get a cell.

Cheers

Hi @kudracha,

That’s a good idea, but Google Sheets is not powerful enough to convert binary numbers made of more than 10 digits. My binary number is 41-digit long. Any other idea?

Hi @Donald_Mitchell,

Here’s an input sample:

And here’s an output sample:

@JBJ ,

I don’t know how that binary-to-decimal formula works in Sheet, but you could search for a custom formula that does that. Basically, take the same steps you do in make and write sheet formulas for it. That’ll do the job in less make operations.

Here goes my 1 hour doing sheet formulas, lol : shared sheet

To do it totally in Make, going binary to decimal, you can use these 6 modules/6 Ops.

Start with input →

Repeater from 0 to length of binary string (41 in this case) →

Text Aggregator to extract each digit of the binary string into a CSV string →

Iterator to split the string by comma and iterate on each one →

Text Aggregator with these settings to create a math equation →


For each digit in the sequence, you need to take 2 to the power of it’s position in the sequence from right to left starting with 0 then multiply that by the digit itself. Then, add all those together, all 41 of them.

In this case the first digit is a 1 and it’s in position is 40 so we take (2^40) * 1.
The second is also one and it’s in position 39 so we take (2^39) * 1, etc…

The resulting string is a huge equation

Use a Math Evaluate a Math Expression module to evaluate it

And the result is 1725008643072

Checking another binary to decimal converter this calculation appears to be accurate

That timestamp converted to readable is Fri Aug 30 2024

Here’s the code, you can copy and paste this as-is directly into a scenario, this is NOT a blueprint input.

Code
{
    "subflows": [
        {
            "flow": [
                {
                    "id": 131,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "scope": "roundtrip",
                        "variables": [
                            {
                                "name": "Binary Input",
                                "value": "11001000110100010100001110000010000000000"
                            }
                        ]
                    },
                    "metadata": {
                        "designer": {
                            "x": 89,
                            "y": 757
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                },
                                "variables": {
                                    "items": [
                                        null
                                    ]
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "variables",
                                "spec": [
                                    {
                                        "name": "name",
                                        "type": "text",
                                        "label": "Variable name",
                                        "required": true
                                    },
                                    {
                                        "name": "value",
                                        "type": "any",
                                        "label": "Variable value"
                                    }
                                ],
                                "type": "array",
                                "label": "Variables"
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            }
                        ],
                        "interface": [
                            {
                                "name": "Binary Input",
                                "type": "any",
                                "label": "Binary Input"
                            }
                        ]
                    }
                },
                {
                    "id": 124,
                    "module": "builtin:BasicRepeater",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "step": "1",
                        "start": "0",
                        "repeats": "{{length(131.`Binary Input`)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 409,
                            "y": 764
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "start",
                                "type": "number",
                                "label": "Initial value",
                                "required": true
                            },
                            {
                                "name": "repeats",
                                "type": "number",
                                "label": "Repeats",
                                "required": true,
                                "validate": {
                                    "max": 10000,
                                    "min": 0
                                }
                            },
                            {
                                "name": "step",
                                "type": "number",
                                "label": "Step",
                                "required": true
                            }
                        ]
                    }
                },
                {
                    "id": 126,
                    "module": "util:TextAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 124,
                        "rowSeparator": "other",
                        "otherRowSeparator": ","
                    },
                    "mapper": {
                        "value": "{{substring(131.`Binary Input`; 124.i; 124.i + 1)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 748,
                            "y": 770
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "Repeater [124]"
                                }
                            },
                            "parameters": {
                                "rowSeparator": {
                                    "label": "Other"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "rowSeparator",
                                "type": "select",
                                "label": "Row separator",
                                "validate": {
                                    "enum": [
                                        "\n",
                                        "\t",
                                        "other"
                                    ]
                                }
                            },
                            {
                                "name": "otherRowSeparator",
                                "type": "text",
                                "label": "Separator"
                            }
                        ],
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "advanced": true
                    }
                },
                {
                    "id": 127,
                    "module": "builtin:BasicFeeder",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "array": "{{split(126.text; \",\")}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1077,
                            "y": 778
                        },
                        "restore": {
                            "expect": {
                                "array": {
                                    "mode": "edit"
                                }
                            }
                        },
                        "expect": [
                            {
                                "mode": "edit",
                                "name": "array",
                                "spec": [],
                                "type": "array",
                                "label": "Array"
                            }
                        ]
                    }
                },
                {
                    "id": 129,
                    "module": "util:TextAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 127,
                        "rowSeparator": "other",
                        "otherRowSeparator": "+"
                    },
                    "mapper": {
                        "value": "((2^({{127.`__IMTLENGTH__`}}-{{127.`__IMTINDEX__`}}))*{{127.value}})"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1425,
                            "y": 782
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "Iterator [127]"
                                }
                            },
                            "parameters": {
                                "rowSeparator": {
                                    "label": "Other"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "rowSeparator",
                                "type": "select",
                                "label": "Row separator",
                                "validate": {
                                    "enum": [
                                        "\n",
                                        "\t",
                                        "other"
                                    ]
                                }
                            },
                            {
                                "name": "otherRowSeparator",
                                "type": "text",
                                "label": "Separator"
                            }
                        ],
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "advanced": true
                    }
                },
                {
                    "id": 130,
                    "module": "math:EvaluateExpression",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "expression": "{{129.text}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1788,
                            "y": 786,
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "expression",
                                "type": "text",
                                "label": "Expression",
                                "required": true
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}