cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Integer in hex notation?

Integer in hex notation?

LunatiX
New Contributor

How can I change the notation of a Integer value, t.e.:

LunatiX_0-1650898921569.png

(Integer -> hexadecimal notation)

3 REPLIES 3

webiq-sk
Frequent Contributor

Two possible ways:
1. Have the value stored/converted in the PLC already
2. Convert it using a LocalScript that subscribes to the item, converts it to hexadecimal and emits it as a virtual item that will then be connected to the desired widget

HmiGuide
Community Moderator
Community Moderator

There is another way, with the iq-text element which support the execution of JS functions.

  1. Define a Local-Script "Globals" where you can place all your global JS functions, variables and constants.
  2. Remove code, which is automatically created
  3. Insert code
    /**
    Converts an interger number to a HEX string
     * @example
     * x = Int2Hex(255, 4)); // x = "00FF"
     * 
     * @param {number} iVal Number to convert
     * @param {number} iLen length of converted string
     * @returns {string} HEX string of iVal
     */
    function Int2Hex(iVal, iLen) {
        return iVal.toString(16).toUpperCase().padStart(iLen, "0");
    }
    
    /**
     * Converts iVal into binary string
     * @example
     * x = Int2Bin(3, 4)); // x = "0011"
     *
     * @param {number} iVal Number to convert
     * @param {number} iLen length of converted string
     * @returns {string} HEX string of iVal
     */
    function Int2Bin(iVal, iLen) {
        return iVal.toString(2).padStart(iLen, "0");
    }
  4. Insert a iq-text with the INT item to display as HEX number
  5. Not working with virtual items
  6. To display Hex: Insert text: <%= Int2Hex(items[0].value,6) %>
  7. To display Bin: Insert text: <%= Int2Bin(items[0].value,6) %>

Thank you for this function !   I just used it. Works fine.

I had some troubles first because I used the wrong iq-text...

Therfore for beginners like me 🙂

stefanAT_ctrlX_0-1722605314213.png

 

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist