Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Hexdecimal output

Re: Hexdecimal output

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: 1997/04/13
Message-ID: <335132d1.24130688@newshost>#1/1

Heres just such a package to do this. It converts from base 10 to any other base. For example:

SQL> l
  1 select to_hex( 1234 ) hex, to_bin( 1234 ) bin,   2 to_oct( 1234 ) oct, to_base( 1234, 5 ) base5   3* from dual
SQL> /   HEX BIN OCT BASE5
-------- --------------- -------- -------- 4D2 10011010010 2322 14414  

So I have convienence functions for hex, binary, and octal. All other bases up to base 16 are available via the to_base( x, y ) function. Also, to convert from base N to base 10, there is the to_base function...

On Fri, 11 Apr 1997 17:26:52 -0700, kbradley_at_us.oracle.com (Kirk Bradley) wrote:

>
>write yourself a function in pl/sql to do it.. since oracle numbers
>are not like machine integers you could end up with many hex digits
>to represent something..
>
>
>In article <334D1570.13A3_at_detroitedison.com>, lechkung_at_detroitedison.com wrote:
>
>> Hi,
>> This may seem very easy to most of you out there, but how do you output
>> a NUMBER in a hexdecimal form??
>>
>> ex: $ SELECT status_flag FROM remote_control_units;
>> ^
>> |
>> +---- This is what I want in hex form (0xfffffff)
>>
>> Thanks
>> Greg Lechkun
>> lechkung_at_detroitedison.com
>> lechkung_at_tir.com

create or replace 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 PROGRAM_ERROR;
	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;
/

create or replace 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;
/
show errors

create or replace function to_hex( p_dec in number ) return varchar2 is
begin

        return to_base( p_dec, 16 );
end to_hex;
/
create or replace function to_bin( p_dec in number ) return varchar2 is
begin

        return to_base( p_dec, 2 );
end to_bin;
/
create or replace function to_oct( p_dec in number ) return varchar2 is
begin

        return to_base( p_dec, 8 );
end to_oct;
/

Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Bethesda MD

http://govt.us.oracle.com/ -- downloadable utilities



Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Sun Apr 13 1997 - 00:00:00 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US