--十进制转二进制
create or replace function number_2_bit(v_num number) return varchar is
v_rtn varchar(2000);
v_n1 number;
v_n2 number;
begin
v_n1 := v_num;
loop
v_n2 := mod(v_n1, 2);
v_n1 := trunc(v_n1 / 2);
v_rtn := to_char(v_n2) || v_rtn;
exit when v_n1 = 0;
end loop;
return v_rtn;
end;
--二进制转十进制
create or replace function bit_2_number(v_char varchar) return number is
v_rtn number(10);
v_n1 number;
v_n2 number;
begin
SELECT SUM(data1)
into v_rtn
FROM (SELECT substr(v_char, rownum, 1) *
power(2, length(v_char) - rownum) data1
FROM dual
CONNECT BY rownum <= length(v_char));
return v_rtn;
end;
|