number to binary conversion

2001-06-14 Thread Steve Haas

Good afternoon,

I have seen on this list some time ago PL/SQL to convert a number to/from
decimal and hex.
Does anyone have PL/SQL to convert a number to/from a binary representation?

TIA,

Steven Haas
Opus Consultants, LLC
"Opinions expressed, right or wrong, are my own."


-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Steve Haas
  INET: [EMAIL PROTECTED]

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: [EMAIL PROTECTED] (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).



RE: number to binary conversion

2001-06-14 Thread Larry Elkins

Steven,

For decimal to hex, a to_char function using the "X" element can be used.
There are certain restrictions -- refer to the Number Formats table in the
SQL manual for more info. Here is an example:

  1* select to_char(10,'XXX') from dual
SQL> /

TO_C

   A

For a more robust solution that goes both ways, and, deals with conversions
between various bases, take a look at:

http://govt.oracle.com/~tkyte/hexdec/index.html


Regards,

Larry G. Elkins
[EMAIL PROTECTED]
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Steve Haas
> Sent: Thursday, June 14, 2001 4:02 PM
> To: Multiple recipients of list ORACLE-L
> Subject: number to binary conversion
>
>
> Good afternoon,
>
> I have seen on this list some time ago PL/SQL to convert a number to/from
> decimal and hex.
> Does anyone have PL/SQL to convert a number to/from a binary
> representation?
>
> TIA,
>
> Steven Haas
> Opus Consultants, LLC
> "Opinions expressed, right or wrong, are my own."

-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Larry Elkins
  INET: [EMAIL PROTECTED]

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: [EMAIL PROTECTED] (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).



Re: number to binary conversion

2001-06-14 Thread Igor Neyman

Here is my modification of Tom's Kyte code:

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 ) then
 raise PROGRAM_ERROR;
end if;

IF (p_dec < 0) THEN
 l_num := l_num + 1;
 loop
  l_str := substr( l_hex, l_num - p_base*FLOOR(l_num/p_base), 1 ) || l_str;
  l_num := trunc( l_num/p_base );
  exit when ( l_num = 0 );
 end loop;
ELSE
 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;
END IF;
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;


Igor Neyman, OCP DBA
Perceptron, Inc.
(734)414-4627
[EMAIL PROTECTED]


- Original Message -
To: "Multiple recipients of list ORACLE-L" <[EMAIL PROTECTED]>
Sent: Thursday, June 14, 2001 5:01 PM


> Good afternoon,
>
> I have seen on this list some time ago PL/SQL to convert a number to/from
> decimal and hex.
> Does anyone have PL/SQL to convert a number to/from a binary
representation?
>
> TIA,
>
> Steven Haas
> Opus Consultants, LLC
> "Opinions expressed, right or wrong, are my own."
>
>
> --
> Please see the official ORACLE-L FAQ: http://www.orafaq.com
> --
> Author: Steve Haas
>   INET: [EMAIL PROTECTED]
>
> 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: [EMAIL PROTECTED] (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).

-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Igor Neyman
  INET: [EMAIL PROTECTED]

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: [EMAIL PROTECTED] (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).



Re: number to binary conversion

2001-06-15 Thread Connor McDonald

Simple one attached...Handles up to 32bits, and I
could squeeze about 3000 conversions per second out of
my laptop with it.

Cheers
Connor

create or replace
package conv is
  function to_binary(p_in number) return varchar2;
  function from_binary(p_in varchar2) return number;
end;
/

create or replace
package body conv is

function to_binary(p_in number) return varchar2 is
  v_result varchar2(60);
  v_temp number := p_in;
  large_power_2 number(20) := power(2,31);  -- max
bits-1
begin
  for i in 1 .. 32 loop -- must match
number of bits
if v_temp >= large_power_2 then
  v_result := v_result || '1';
  v_temp := v_temp - large_power_2;
else
  v_result := v_result || '0';
end if;
large_power_2 := trunc(large_power_2/2);
  end loop;
  return v_result;
end;

function from_binary(p_in varchar2) return number is
  v_result number := 0;
  power_2 number(20) := 1;
begin
  for i in reverse 1 .. length(p_in) loop
v_result := v_result +
to_number(substr(p_in,i,1))*power_2;
power_2 := power_2 * 2;
  end loop;
  return v_result;
end;

end;
/



--- Steve Haas <[EMAIL PROTECTED]> wrote: > Good
afternoon,
> 
> I have seen on this list some time ago PL/SQL to
> convert a number to/from
> decimal and hex.
> Does anyone have PL/SQL to convert a number to/from
> a binary representation?
> 
> TIA,
> 
> Steven Haas
> Opus Consultants, LLC
> "Opinions expressed, right or wrong, are my own."
> 
> 
> -- 
> Please see the official ORACLE-L FAQ:
> http://www.orafaq.com
> -- 
> Author: Steve Haas
>   INET: [EMAIL PROTECTED]
> 
> 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: [EMAIL PROTECTED] (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).


=
Connor McDonald
http://www.oracledba.co.uk (mirrored at 
http://www.oradba.freeserve.co.uk)

"Some days you're the pigeon, some days you're the statue"


Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: =?iso-8859-1?q?Connor=20McDonald?=
  INET: [EMAIL PROTECTED]

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: [EMAIL PROTECTED] (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).