一 16進制轉換為10進制
可以通過to_number函數實現
SQL> select to_number(’19f’,’xxx’) from dual;
TO_NUMBER(’19F’,’XXX’)
----------------------
415
SQL> select to_number(’f’,’xx’) from dual;
TO_NUMBER(’F’,’XX’)
-------------------
15
二 10進制轉換為16進制
可以通過to_char函數轉換
SQL> select to_char(123,’xxx’) from dual;
TO_C
----
7b
SQL> select to_char(4567,’xxxx’) from dual;
TO_CH
-----
11d7
三 2進制轉換為10進制
從Oracle9i開始,提供函數bin_to_num進行2進制到10進制的轉換
SQL> select bin_to_num(1,1,0,1) a,bin_to_num(1,0) b from dual;
A B
----- ----------
13 2
SQL> select bin_to_num(1,1,1,0,1) from dual;
BIN_TO_NUM(1,1,1,0,1)
---------------------
29
四 通過自定義函數實現進制轉換
以下函數來自AskTom網站,是Tom給出的例子,供參考:
create or replace function to_base( p_dec in number, p_base in number )
return varchar2
is
l_str varchar2(255) default NULL;
l_num number default p_dec;
l_hex varchar2(16) default ’0123456789ABCDEF’;
begin
if ( trunc(p_dec) <> p_dec OR p_dec < 0 ) then
raise PROGRAM_ERROR;
end if;
loop
l_str := substr( l_hex, mod(l_num,p_base)+1, 1 ) || l_str;
l_num := trunc( l_num/p_base );
exit when ( l_num = 0 );
end loop;
return l_str;
end to_base;
/
create or replace function to_dec
( p_str in varchar2,
p_from_base in number default 16 ) return number
is
l_num number default 0;
l_hex varchar2(16) default ’0123456789ABCDEF’;
begin
for i in 1 .. length(p_str) loop
l_num := l_num * p_from_base + instr(l_hex,upper(substr(p_str,i,