xref: /Universal-ctags/Units/parser-sql.r/hex2dec.sql.d/input.sql (revision ac0bc2347d23a84cbef9bc0e024621f9a30eb358)
1rem -----------------------------------------------------------------------
2rem URL:        http://www.orafaq.com/scripts/plsql/hex2dec.txt
3rem Filename:   hex2dec.sql
4rem Purpose:    Functions to convert Hex to Decimal and vice versa
5rem Author:     Mark Malakanov, Feb-1999 + Anonymous
6rem -----------------------------------------------------------------------
7
8CREATE OR REPLACE FUNCTION hex2dec (hexnum in char) RETURN number IS
9  i                 number;
10  digits            number;
11  result            number := 0;
12  current_digit     char(1);
13  current_digit_dec number;
14BEGIN
15  digits := length(hexnum);
16  for i in 1..digits loop
17     current_digit := SUBSTR(hexnum, i, 1);
18     if current_digit in ('A','B','C','D','E','F') then
19        current_digit_dec := ascii(current_digit) - ascii('A') + 10;
20     else
21        current_digit_dec := to_number(current_digit);
22     end if;
23     result := (result * 16) + current_digit_dec;
24  end loop;
25  return result;
26END hex2dec;
27/
28show errors
29
30CREATE OR REPLACE FUNCTION num2hex (N in number) RETURN varchar2 IS
31  H  varchar2(64) :='';
32  N2 integer      := N;
33BEGIN
34  loop
35     select rawtohex(chr(N2))||H
36     into   H
37     from   dual;
38
39     N2 := trunc(N2 / 256);
40     exit when N2=0;
41  end loop;
42  return H;
43END num2hex;
44/
45show errors
46
47-- Examples:
48select hex2dec('FF') from dual;
49
50select num2hex(10) from dual;
51