SET TERM ^ ;

CREATE OR ALTER function FN_INTEGER_TO_HEX (
    C BIGINT
)
RETURNS VARCHAR(32)
AS
DECLARE VARIABLE IREM INTEGER;
DECLARE VARIABLE HEX  VARCHAR(32);
BEGIN

    IREM = MOD(C, 0XFF);
    HEX = '';

    WHILE (IREM > 0) DO
    BEGIN

        HEX = SUBSTRING('0123456789abcdef' FROM BIN_SHR(BIN_AND(C, 0XFF), 4) + 1 FOR 1) || SUBSTRING('0123456789abcdef' FROM BIN_AND(BIN_AND(C, 0XFF), 15) + 1 FOR 1) || HEX;

        C = BIN_SHR(C, 8);

        IREM = MOD(C, 0XFF);

    END


    IF (TRIM(HEX) <> '') THEN
    BEGIN
        HEX = '#'||HEX;
    END


    RETURN TRIM(HEX);
END
^

SET TERM ; ^

