分类: Oracle
2008-04-24 15:41:23
作者:oracle11g 来自:oracle11g.itpub.net
create or replace package bitops2 is
function bitand(p_dec1 number, p_dec2 number) return varchar2 ;
function bitor(p_dec1 number, p_dec2 number) return varchar2 ;
function bitxor(p_dec1 number, p_dec2 number) return varchar2 ;
function raw_ascii(p_dec number) return raw;
function ascii_raw(p_raw varchar2) return number;
function bitnot(p_dec1 number) return number;
end;
create or replace package body bitops2 is
function raw_ascii(p_dec number) return raw is
v_result varchar2(1999);
v_tmp1 number := p_dec;
begin
loop
v_result := chr(mod(v_tmp1,256)) || v_result ;
v_tmp1 := trunc(v_tmp1/256);
exit when v_tmp1 = 0;
end loop;
return utl_raw.cast_to_raw(v_result);
end;
function ascii_raw(p_raw varchar2) return number is
v_result number := 0;
begin
for i in 1 .. length(p_raw) loop
v_result := v_result * 256 + ascii(substr(p_raw,i,1));
end loop;
return v_result;
end;
function bitand(p_dec1 number, p_dec2 number) return varchar2 is
begin
return
ascii_raw(
utl_raw.cast_to_varchar2(
utl_raw.bit_and(
raw_ascii(p_dec1),
raw_ascii(p_dec2)
)
)
);
end;
function bitor(p_dec1 number, p_dec2 number) return varchar2 is
begin
return
ascii_raw(
utl_raw.cast_to_varchar2(
utl_raw.bit_or(
raw_ascii(p_dec1),
raw_ascii(p_dec2)
)
)
);
end;
function bitxor(p_dec1 number, p_dec2 number) return varchar2 is
begin
return
ascii_raw(
utl_raw.cast_to_varchar2(
utl_raw.bit_xor(
raw_ascii(p_dec1),
raw_ascii(p_dec2)
)
)
);
end;
function bitnot(p_dec1 number) return number is
begin
return (0 - p_dec1) - 1;
end;
end;