Quantcast
Channel: SCN: Message List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 9165

Re: HEX String to INTEGER conversion

$
0
0

To help myself with an interim solution I came up with the following procedure to convert a HEX string to BIGINT:

 

CREATE PROCEDURE hex2int (IN i_hex VARCHAR, OUT o_result BIGINT) 

LANGUAGE SQLSCRIPT 

SQL SECURITY INVOKER AS

  pos INTEGER := 1;

  hex_len INTEGER;

  current_digit CHAR;

  current_val INTEGER;

  result BIGINT := 0;

BEGIN

  DECLARE hex2int CONDITION FOR SQL_ERROR_CODE 10001;

  DECLARE EXIT HANDLER FOR hex2int RESIGNAL;

  hex_len := LENGTH(:i_hex);

  WHILE pos <= hex_len DO

       result := result * 16;

       current_digit := SUBSTR(:i_hex, pos, 1);

       -- format checking

       IF NOT ((current_digit >= 'A' and current_digit <= 'F') or

              (current_digit >= '0' and current_digit <= '9')) THEN

          SIGNAL hex2int SET MESSAGE_TEXT =

                'Invalid hex cipher: ' || current_digit || ' at position ' || pos;

       END IF;

       current_val := MOD(to_number(to_binary(current_digit)),30);

       IF current_val >= 11 THEN

            result := result + current_val - 1;

       ELSE

            result := result + current_val;

       END IF;

       pos := pos + 1;

  END WHILE;

  o_result := result;

END;

Test Call:

CALL hex2int('FF', ?);

 

Any hints to express this typconversion directly without using a stored procedure?


Viewing all articles
Browse latest Browse all 9165

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>