Requires Free Membership to View
create or replace package Lang_Integer as
/* The package is named loosely after a similar Java class,
java.lang.Integer; in addition, all public package functions
(except toRadixString() which has no Java equivalent) are named
after equivalent Java methods in the java.lang.Integer class.
*/
/* Convert a number to string in given radix.
Radix must be in the range [2, 16].
*/
function toRadixString(num in number, radix in number) return varchar2;
pragma restrict_references (toRadixString, WNDS, WNPS, RNDS, RNPS);
/* Convert a number to binary string. */
function toBinaryString(num in number) return varchar2;
pragma restrict_references (toBinaryString, WNDS, WNPS, RNDS, RNPS);
/* Convert a number to hexadecimal string. */
function toHexString(num in number) return varchar2;
pragma restrict_references (toHexString, WNDS, WNPS, RNDS, RNPS);
/* Convert a number to octal string. */
function toOctalString(num in number) return varchar2;
pragma restrict_references (toOctalString, WNDS, WNPS, RNDS, RNPS);
/* Convert a string, expressed in decimal, to number. */
function parseInt(s in varchar2) return number;
pragma restrict_references (parseInt, WNDS, WNPS, RNDS, RNPS);
/* Convert a string, expressed in given radix, to number.
Radix must be in the range [2, 16].
*/
function parseInt(s in varchar2, radix in number) return number;
pragma restrict_references (parseInt, WNDS, RNDS);
end Lang_Integer;
/
create or replace package body Lang_Integer as
/* Takes a number between 0 and 15, and converts it to a string (character)
The toRadixString() function calls this function.
The caller of this function is responsible for making sure no invalid
number is passed as the argument. Valid numbers include non-negative
integer in the radix used by the calling function. For example,
toOctalString() must pass nothing but 0, 1, 2, 3, 4, 5, 6, and 7 as the
argument 'num' of digitToString().
*/
function digitToString(num in number) return varchar2 as
digitStr varchar2(1);
begin
if (num<10) then
digitStr := to_char(num);
else
digitStr := chr(ascii('A') + num - 10);
end if;
return digitStr;
end digitToString;
/* Takes a character (varchar2(1)) and converts it to a number.
The parseInt() function calls this function.
The caller of this function is responsible for maksing sure no invalid
string is passed as the argument. The caller can do this by first
calling the isValidNumStr() function.
*/
function digitToDecimal(digitStr in varchar2) return number as
num number;
begin
if (digitStr >= '0') and (digitStr <= '9') then
num := ascii(digitStr) - ascii('0');
elsif (digitStr >= 'A') and (digitStr <= 'F') then
num := ascii(digitStr) - ascii('A') + 10;
end if;
return num;
end digitToDecimal;
/* Checks if the given string represents a valid number in given radix.
Returns true if valid; ORA-6502 if invalid.
*/
function isValidNumStr(str in out varchar2,radix in number) return boolean
as
validChars varchar2(16) := '0123456789ABCDEF';
valid number;
len number;
i number;
retval boolean;
begin
if (radix<2) or (radix>16) or (radix!=trunc(radix)) then
i := to_number('invalid number'); /* Forces ORA-6502 when bad radix. */
end if;
Click for part 2 of this script.
This was first published in June 2004

Join the conversationComment
Share
Comments
Results
Contribute to the conversation