Decimal to Binary Arcade Function

808
3
Jump to solution
04-09-2020 03:28 PM
KristinJ
New Contributor III

I’m interested in using Attribute Rules to calculate a field from decimal to binary on insert.  I’m surprised that attribute rules don’t also support using python, but I digress already.  My question is has anyone used Arcade to convert from decimal to binary?  There doesn’t appear to be an existing bin(), or like in Excel dec2bin(), function in Arcade – is this on the road map for Arcade?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Kristin Judy ,

There is no standard Arcade function to do this currently and I have no idea if this is on the backlog. Something that might get you started is the code below. Please note that this does not handle the number of characters nor negative values as is done by Excel dec2bin():

function convertDecimalToBinary(n) {
    // based on https://oamatech.com/javascript-decimal-to-binary-tutorial/
    var binary = "";
    var temp = n;
    for (var i = 0; i < 100; i++) {
        if (temp % 2 == 0) {
            binary = "0" + binary;
        }
        else {
            binary = "1" + binary;
        }
        temp = Floor(temp / 2);
        if (temp <= 0) {
            return binary;
        }
    }
}

return convertDecimalToBinary(20);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Can you explain a little more what usage you have for this requirement?

View solution in original post

0 Kudos
3 Replies
XanderBakker
Esri Esteemed Contributor

Hi Kristin Judy ,

There is no standard Arcade function to do this currently and I have no idea if this is on the backlog. Something that might get you started is the code below. Please note that this does not handle the number of characters nor negative values as is done by Excel dec2bin():

function convertDecimalToBinary(n) {
    // based on https://oamatech.com/javascript-decimal-to-binary-tutorial/
    var binary = "";
    var temp = n;
    for (var i = 0; i < 100; i++) {
        if (temp % 2 == 0) {
            binary = "0" + binary;
        }
        else {
            binary = "1" + binary;
        }
        temp = Floor(temp / 2);
        if (temp <= 0) {
            return binary;
        }
    }
}

return convertDecimalToBinary(20);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Can you explain a little more what usage you have for this requirement?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Since the requirement was to get a 16 bit number, the last line can be changed to (replace "8223" with the variable field you want to apply the conversion to):

return Text(Number(convertDecimalToBinary(8223)), "0000000000000000");
0 Kudos
KristinJ
New Contributor III

Thanks, Xander Bakker! This is exactly what I’m looking for.  We’re using GeoEvent Server to update feature services with vehicle location and path data.  I’ll use this to convert a decimal “Status” value to binary on insert using Attribute Rules.  This will allow us to extract information from the “Status” value provided by our AVL vendor to determine what vehicles are actually doing, not just where they are, e.g. street sweeping broom up or down (1=Low, 0=High).  Per suggestion I’ve included the text and number functions to include leading zeros so we get a complete 16-bit binary number.  Thanks again!

0 Kudos