SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.CONVERT_HEX_TO_CHAR ( 
	HEX_STRING VARCHAR(32000) ) 
	RETURNS VARCHAR(16000)   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.CONVERT_HEX_TO_CHAR 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *CHG , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SRTSEQ = *HEX   
	BEGIN ATOMIC 
DECLARE WRESULT VARCHAR ( 16000 ) DEFAULT '' ; 
DECLARE I INTEGER DEFAULT 1 ; 
DECLARE LEN INTEGER ; 
DECLARE PAIR CHAR ( 2 ) ; 
DECLARE DECIMAL_VAL INTEGER ; 
DECLARE CHAR_VAL CHAR ( 1 ) ; 
-- Handle null input 
IF HEX_STRING IS NULL THEN 
RETURN '' ; 
END IF ; 
SET LEN = LENGTH ( TRIM ( HEX_STRING ) ) ; 
-- Ensure even length (hex pairs) 
IF MOD ( LEN , 2 ) <> 0 THEN 
SET LEN = LEN - 1 ; 
END IF ; 
WHILE I <= LEN DO 
SET PAIR = UPPER ( SUBSTR ( HEX_STRING , I , 2 ) ) ; 
-- Convert hex pair to decimal 
SET DECIMAL_VAL = 
CASE SUBSTR ( PAIR , 1 , 1 ) 
WHEN '0' THEN 0 
WHEN '1' THEN 16 
WHEN '2' THEN 32 
WHEN '3' THEN 48 
WHEN '4' THEN 64 
WHEN '5' THEN 80 
WHEN '6' THEN 96 
WHEN '7' THEN 112 
WHEN '8' THEN 128 
WHEN '9' THEN 144 
WHEN 'A' THEN 160 
WHEN 'B' THEN 176 
WHEN 'C' THEN 192 
WHEN 'D' THEN 208 
WHEN 'E' THEN 224 
WHEN 'F' THEN 240 
ELSE 0 
END + 
CASE SUBSTR ( PAIR , 2 , 1 ) 
WHEN '0' THEN 0 
WHEN '1' THEN 1 
WHEN '2' THEN 2 
WHEN '3' THEN 3 
WHEN '4' THEN 4 
WHEN '5' THEN 5 
WHEN '6' THEN 6 
WHEN '7' THEN 7 
WHEN '8' THEN 8 
WHEN '9' THEN 9 
WHEN 'A' THEN 10 
WHEN 'B' THEN 11 
WHEN 'C' THEN 12 
WHEN 'D' THEN 13 
WHEN 'E' THEN 14 
WHEN 'F' THEN 15 
ELSE 0 
END ; 
-- Convert EBCDIC to ASCII using correct mappings 
SET CHAR_VAL = 
CASE DECIMAL_VAL 
-- EBCDIC to ASCII character mappings 
WHEN 64 THEN ' '  -- Space 
WHEN 75 THEN '.'  -- Period 
WHEN 76 THEN '<'  -- Less than 
WHEN 77 THEN '('  -- Left parenthesis 
WHEN 78 THEN '+'  -- Plus sign 
WHEN 79 THEN '|'  -- Vertical bar 
WHEN 80 THEN '&'  -- Ampersand 
WHEN 90 THEN '!'  -- Exclamation mark 
WHEN 91 THEN '$'  -- Dollar sign 
WHEN 92 THEN '*'  -- Asterisk 
WHEN 93 THEN ')'  -- Right parenthesis 
WHEN 94 THEN ';'  -- Semicolon 
WHEN 96 THEN '-'  -- Hyphen/minus 
WHEN 97 THEN '/'  -- Slash 
WHEN 107 THEN ','  -- Comma 
WHEN 108 THEN '%'  -- Percent 
WHEN 109 THEN '_'  -- Underscore 
WHEN 110 THEN '>'  -- Greater than 
WHEN 111 THEN '?'  -- Question mark 
WHEN 122 THEN ':'  -- Colon 
WHEN 123 THEN '#'  -- Number sign 
WHEN 124 THEN '@'  -- At sign 
WHEN 125 THEN ''''  -- Apostrophe 
WHEN 126 THEN '='  -- Equals sign 
WHEN 127 THEN '"'  -- Quotation mark 
-- Letters A-I (EBCDIC C1-C9) 
WHEN 193 THEN 'A' 
WHEN 194 THEN 'B' 
WHEN 195 THEN 'C' 
WHEN 196 THEN 'D' 
WHEN 197 THEN 'E' 
WHEN 198 THEN 'F' 
WHEN 199 THEN 'G' 
WHEN 200 THEN 'H' 
WHEN 201 THEN 'I' 
-- Letters J-R (EBCDIC D1-D9) 
WHEN 209 THEN 'J' 
WHEN 210 THEN 'K' 
WHEN 211 THEN 'L' 
WHEN 212 THEN 'M' 
WHEN 213 THEN 'N' 
WHEN 214 THEN 'O' 
WHEN 215 THEN 'P' 
WHEN 216 THEN 'Q' 
WHEN 217 THEN 'R' 
-- Letters S-Z (EBCDIC E2-E9) 
WHEN 226 THEN 'S' 
WHEN 227 THEN 'T' 
WHEN 228 THEN 'U' 
WHEN 229 THEN 'V' 
WHEN 230 THEN 'W' 
WHEN 231 THEN 'X' 
WHEN 232 THEN 'Y' 
WHEN 233 THEN 'Z' 
-- Numbers 0-9 (EBCDIC F0-F9) 
WHEN 240 THEN '0' 
WHEN 241 THEN '1' 
WHEN 242 THEN '2' 
WHEN 243 THEN '3' 
WHEN 244 THEN '4' 
WHEN 245 THEN '5' 
WHEN 246 THEN '6' 
WHEN 247 THEN '7' 
WHEN 248 THEN '8' 
WHEN 249 THEN '9' 
-- Lowercase letters a-i (EBCDIC 81-89) 
WHEN 129 THEN 'a' 
WHEN 130 THEN 'b' 
WHEN 131 THEN 'c' 
WHEN 132 THEN 'd' 
WHEN 133 THEN 'e' 
WHEN 134 THEN 'f' 
WHEN 135 THEN 'g' 
WHEN 136 THEN 'h' 
WHEN 137 THEN 'i' 
-- Lowercase letters j-r (EBCDIC 91-99) 
WHEN 145 THEN 'j' 
WHEN 146 THEN 'k' 
WHEN 147 THEN 'l' 
WHEN 148 THEN 'm' 
WHEN 149 THEN 'n' 
WHEN 150 THEN 'o' 
WHEN 151 THEN 'p' 
WHEN 152 THEN 'q' 
WHEN 153 THEN 'r' 
-- Lowercase letters s-z (EBCDIC A2-A9) 
WHEN 162 THEN 's' 
WHEN 163 THEN 't' 
WHEN 164 THEN 'u' 
WHEN 165 THEN 'v' 
WHEN 166 THEN 'w' 
WHEN 167 THEN 'x' 
WHEN 168 THEN 'y' 
WHEN 169 THEN 'z' 
-- For unmapped characters, return space 
ELSE ' ' 
END ; 
SET WRESULT = WRESULT || CHAR_VAL ; 
SET I = I + 2 ; 
END WHILE ; 
RETURN WRESULT ; 
END  ; 

COMMENT ON PARAMETER SPECIFIC FUNCTION CISTOOLS.CONVERT_HEX_TO_CHAR 
( HEX_STRING IS 'Hexadecimal String' ) ; 

LABEL ON SPECIFIC FUNCTION CISTOOLS.CONVERT_HEX_TO_CHAR 
	IS 'Convert Hex To Char' ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.CONVERT_HEX_TO_CHAR 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.CONVERT_HEX_TO_CHAR 
TO PUBLIC ; 

