conversione esadecimale - decimale e
formato COMP-3 (cobol)
create or replace function fn_hex2dec (ix varchar2 ) return number is
l smallint;
ris number;
f number;
x varchar2(40);
function hdec (x char ) return smallint is
y smallint;
begin
if x in ('0','1','2','3','4','5','6','7','8','9') then
y:= x;
elsif x in('a','b','c','d','e','f') then
y:= ascii(x) -87;
else
y:= null;
end if;
return y;
end;
begin
x:=lower(ix);
l:= length(x) ;
f:= 1;
ris :=0;
while l>0 loop
ris := ris + hdec(substr(x,l,1))*f;
f:=f*16;
l:=l-1;
end loop;
return ris;
end;
/
create or replace function fn_comp3(x number , ncifre smallint, autoconv char default 's') return varchar2 is
y varchar2(30);
xs varchar2(30);
s varchar2(30);
l smallint;
ndec smallint;
segno char;
xcifre smallint :=ncifre;
begin
if xcifre mod 2 =0 then
xcifre := xcifre+1;
end if;
xs := ltrim(to_char( nvl(x,0) , rpad('0',xcifre,'9'))) ;
----- nel caso di numeri che non rientrano nel numero di cifre specificato, xs risulta '##..##' , la fn_hex2dec ritorna quindi null e la
----- fn_trasc ('da-mvs') segnala l'errore
l:= length(xs) ;
if x<0 then segno :='D';
else segno :='C';
end if;
while l>0 loop
s:= substr (xs,l);
xs:= substr (xs,1,l-1);
if length(s)=1 then
s:=s|| segno ;
end if;
ndec:=fn_hex2dec (s) ;
if ndec = 0 then
ndec :=204; ------- necessario * probl con chr(0) = fine stringa
end if;
y := chr(ndec ) || y ;
l:=l-2;
end loop;
if autoconv='s' then
y:= fn_trasc(y, 'da-mvs');
end if;
return y;
--------- y := hextoraw( x||'C'); ma non funziona..
end;
/
select * from tabella1
/
|
create or replace function fn_hex2dec (ix varchar2 ) return number is
l sma... errori:
create or replace function fn_comp3(x number , ncifre smallint, autoconv... errori:
select * from tabella1
|