ORACLE 36进制和10进制,互相转换函数
第一部分
--36转10进制
create or replace function f_36to10 (str varchar) return int is
returnValue int;
str36 varchar(36);
subWork varchar(1);
workIndex int;
len int;
i int;
begin
returnValue:= 0;
str36 := '123456789ABCDEFGHIJKLMNOPQRSTUVWSYZ';
i := 1;
len := length(trim(str));
while i <= len loop
subWork := SUBSTR(str, i, 1);
workIndex := instr( str36,subWork,1,1);
returnValue := returnValue + (workIndex * power(36, len-i));
i := i + 1;
end loop;
return returnValue;
end f_36to10;
第二部分
--10转36进制
create or replace function f_10to36(i int) return varchar is
ret varchar2(8);
var_m int;
var_s int;
begin
ret := '';
var_s := i;
while var_s >= 36 loop
var_m := mod(var_s, 36);
if var_m < 10 then
ret := to_char(var_m) || ret;
else
ret := chr(var_m - 10 + 97) || ret;
end if;
var_s := trunc(var_s / 36);
end loop;
if var_s > 0 or (var_s = 0 and ret = '') then
if var_s < 10 then
ret := to_char(var_s) || ret;
else
ret := chr(var_s - 10 + 97) || ret;
end if;
end if;
return UPPER(ret);--lpad(ret, 8, '0');
end f_10to36;
另外一个方法,链接:http://www.cnblogs.com/jak-black/archive/2010/09/25/1834617.html