Is there a piece of PL/SQL or function or group of functions where I can take a number between 1 and 1234567 and convert it into a HEX value?

    Requires Free Membership to View

Script continued from part 1.

    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; 

This was first published in June 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: