How to convert decimal number into binary number in Azure data explorer?

Shubham Jaunjal 0 Reputation points
2024-01-19T09:56:55.3+00:00

I not able to find adx decimal number conversion function. i am trying to convert 20321 [decimal] to binary

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
501 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 30,711 Reputation points MVP
    2024-01-19T17:53:54.7666667+00:00

    Hello @Shubham Jaunjal,

    welcome to this moderated Azure community forum.

    Both Azure Data Explorer and the KQL query language do not support a decimal number to binary value conversion.

    There is also no support for both the byte or byte array data type. You probably need to convert to a string.

    The closest conversion is the 'tohex()' function which returns a hexadecimal conversion. this could be the first step into the conversion into a binary format string.

    An alternative is making use of a function written in the Python language and available as a plugin for Azure Data Explorer.

    Notice you need to enable the Python plugin first at the level of the cluster: User's image

    Notice that the first execution will be slow because the plugin must be loaded. A possible solution based on Python is:

      range intValue from 20321 to 20321 step 1
      | evaluate python(
      //
      typeof(*, res:string),
    

    result = df result["res"] = bin(int(df["intValue"]))

      bag_pack("dummy",42)
      )
    

    The result looks like: User's image

    Please check for yourself if this is a plausible solution (activating Python) and offers enough execution performance.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


  2. Slaghuis, Eric 0 Reputation points
    2024-02-13T14:01:25.8033333+00:00

    Here is an alternative that does not rely on Python. The function takes a hex string that could easily be created with the native tohex() function. It basically does a text replace of the hex string.

    let HexToBinary = (hex:string) { 
        replace_strings(
            hex,
            dynamic(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']), // Lookup strings
            dynamic(['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']) // Replacements
        )
    };
    print HexToBinary(tohex(51767))
    
    0 comments No comments