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: Hexa to binary number

Re: Hexa to binary number

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: Fri, 11 Jun 1999 20:12:35 GMT
Message-ID: <376d6d50.17774328@newshost.us.oracle.com>


A copy of this was sent to mquesada_at_renault.com.ar (if that email address didn't require changing) On Fri, 11 Jun 1999 18:45:10 GMT, you wrote:

>Hi:
> I need to transform an Hexadecimal number to a binary number
>for example :
> Nr hexa Nr Binary
> BC 10111100
>
>There is some funtion to do this ??
>
> Thank you
>
>Marcelo Quesada
>mquesada_at_renault.com.ar
>
>
>Sent via Deja.com http://www.deja.com/
>Share what you know. Learn what you don't.

If you install the following, you'll have everything (and more) you need. You'll be able to:

SQL> select to_bin( to_dec( 'bc' ) ) from dual;

TO_BIN(TO_DEC('BC'))



10111100

for example....

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;
/

See http://www.oracle.com/ideveloper/ for my column 'Digging-in to Oracle8i'... Mirrored (and more current) at http://govt.us.oracle.com/~tkyte/

Current article is "Fine Grained Access Control", added June 8'th  

Thomas Kyte                   tkyte_at_us.oracle.com
Oracle Service Industries     Reston, VA   USA
--
Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Fri Jun 11 1999 - 15:12:35 CDT

Original text of this message

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