Converting number to HEX value, part 2
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
str := upper(str); /* a-f ==> A-F */ /* determine valid characters for given radix */ validChars := substr('0123456789ABCDEF', 1, radix); valid := 1; len := length(str); i := 1; while (valid !=0) loop valid := instr(validChars, substr(str, i, 1)); i := i + 1; end loop; if (valid=0) then retval := false; i := to_number('invalid number'); /* Forces ORA-6502. */ else retval := true; end if; return retval; end isValidNumStr; /* This function converts a number into a string in given radix. Only non-negative integer should be passed as the argument num, and radix must be a positive integer in [1, 16]. Otherwise, 'ORA-6502: PL/SQL: numeric or value error' is raised. */ function toRadixString(num in number, radix in number) return varchar2 as dividend number; divisor number; remainder number(2); numStr varchar2(2000); begin /* NULL NUMBER -> NULL hex string */ if(num is null) then return null; elsif (num=0) then /* special case */ return '0'; end if; /* invalid number or radix; force ORA-6502: PL/SQL: numeric or value err */ if (num<0) or (num!=trunc(num)) or (radix<2) or (radix>16) or (radix!=trunc(radix)) then numStr := to_char(to_number('invalid number')); /* Forces ORA-6502. */ return numStr; end if; dividend := num; numStr := ''; /* start with a null string */ /* the actual conversion loop */ while(dividend != 0) loop remainder := mod(dividend, radix); numStr := digitToString(remainder) || numStr; dividend := trunc(dividend / radix); end loop; return numStr; end toRadixString; function toBinaryString(num in number) return varchar2 as begin return toRadixString(num, 2); end toBinaryString; function toHexString(num in number) return varchar2 as begin return toRadixString(num, 16); end toHexString; function toOctalString(num in number) return varchar2 as begin return toRadixString(num, 8); end toOctalString; /* The parseInt() function is equivalent to TO_NUMBER() when called without a radix argument. This is consistent with what Java does. */ function parseInt(s in varchar2) return number as begin return to_number(s); end parseInt; /* Converts a string in given radix to a number */ function parseInt(s in varchar2, radix in number) return number as str varchar2(2000); len number; decimalNumber number; begin /* NULL hex string -> NULL NUMBER */ if(s is null) then return null; end if; /* Because isValidNumStr() expects a IN OUT parameter, must use an intermediate variable str. str will be converted to uppercase inside isValidNumStr(). */ str := s; if (isValidNumStr(str, radix) = false) then return -1; /* Never executes because isValidNumStr forced ORA-6502. */ end if; len := length(str); decimalNumber := 0; /* the actual conversion loop */ for i in 1..len loop decimalNumber := decimalNumber*radix + digitToDecimal(substr(str, i, 1)); end loop; return decimalNumber; end parseInt; end Lang_Integer; / grant execute on Lang_Integer to public;
Dig Deeper on Using Oracle PL-SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.
Meet all of our Oracle Database / Applications experts
View all Oracle Database / Applications questions and answers
Start the conversation
0 comments