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

I do not know of any built-ins in Oracle. However, a long time ago I stumbled into a package in the public domain that you can use. I have provided the source code below. I have used this package and I am publishing it here for your benefit without infringing on any copyright.
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

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: