Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Mailing Lists -> Oracle-L -> Re: Decimal to Hex conversion
On Thu, 8 Feb 2001, Deepak Sharma wrote:
>
> There probably is a package called base_convert. I
> haven't used it but it showed up on Metalink, try it
> out.
>
> -- Deepak
Here's a modified version of one from Tom Kyte.
He had separate functions, I prefer a package.
Jared
select radix.to_hex(285) from dual; select radix.to_dec('11D', 16) from dual; select radix.to_bin(255) from dual; select radix.to_bin(256) from dual; -----------------------
create or replace package radix
is
/* courtesy of Thomas Kyte */ function to_base( p_dec in number, p_base in number ) return varchar2; function to_dec ( p_str in varchar2, p_from_base in number default 16 ) return number; function to_hex( p_dec in number ) return varchar2; function to_bin( p_dec in number ) return varchar2; function to_oct( p_dec in number ) return varchar2; pragma restrict_references( to_base, wnds, rnds, wnps, rnps ); pragma restrict_references( to_dec, wnds, rnds, wnps, rnps ); pragma restrict_references( to_hex, wnds, rnds, wnps, rnps ); pragma restrict_references( to_bin, wnds, rnds, wnps, rnps ); pragma restrict_references( to_oct, wnds, rnds, wnps, rnps );
end radix;
/
show errors
create or replace package body radix
is
function to_base( p_dec in number, p_base in number ) return varchar2 is l_str varchar2(255) default NULL; l_num number default p_dec; l_hex varchar2(16) default '0123456789ABCDEF'; begin if ( trunc(p_dec) <> p_dec OR p_dec < 0 ) then raise INVALID_NUMBER; end if; loop l_str := substr( l_hex, mod(l_num,p_base)+1, 1 ) || l_str; l_num := trunc( l_num/p_base ); exit when ( l_num = 0 ); end loop; return l_str; end to_base; function to_dec ( p_str in varchar2, p_from_base in number default 16 ) return number is l_num number default 0; l_hex varchar2(16) default '0123456789ABCDEF'; begin for i in 1 .. length(p_str) loop l_num := l_num * p_from_base + instr(l_hex,upper(substr(p_str,i,1)))-1; end loop; return l_num; end to_dec; function to_hex( p_dec in number ) return varchar2 is begin return to_base( p_dec, 16 ); end to_hex; function to_bin( p_dec in number ) return varchar2 is begin return to_base( p_dec, 2 ); end to_bin; function to_oct( p_dec in number ) return varchar2 is begin return to_base( p_dec, 8 ); end to_oct;
end radix;
/
show errors
-- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: INET: jkstill_at_cybcon.com Fat City Network Services -- (858) 538-5051 FAX: (858) 538-5051 San Diego, California -- Public Internet access / Mailing Lists -------------------------------------------------------------------- To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).Received on Thu Feb 08 2001 - 16:14:36 CST