Q

Converting number to HEX value, part 1

This Content Component encountered an error
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?
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

Dig deeper on Using Oracle PL-SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close