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

Home -> Community -> Mailing Lists -> Oracle-L -> Re: number to binary conversion

Re: number to binary conversion

From: Connor McDonald <hamcdc_at_yahoo.co.uk>
Date: Fri, 15 Jun 2001 00:34:24 -0700
Message-ID: <F001.0032AE3C.20010615003106@fatcity.com>

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


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: hamcdc_at_yahoo.co.uk

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 Fri Jun 15 2001 - 02:34:24 CDT

Original text of this message

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