進制轉換是開發中經常需要用到的
一
可以通過to_number函數實現
SQL> select to_number(
TO_NUMBER(
SQL> select to_number(
TO_NUMBER(
二
可以通過to_char函數轉換
SQL> select to_char(
TO_C
SQL> select to_char(
TO_CH
三
從Oracle
SQL> select bin_to_num(
A B
SQL> select bin_to_num(
BIN_TO_NUM(
四 通過自定義函數實現進制轉換
以下函數來自AskTom網站
create or replace function to_base( p_dec in number
return varchar
is
l_str varchar
l_num number default p_dec;
l_hex varchar
begin
if ( trunc(p_dec) <> p_dec OR p_dec <
raise PROGRAM_ERROR;
end if;
loop
l_str := substr( l_hex
l_num := trunc( l_num/p_base );
exit when ( l_num =
end loop;
return l_str;
end to_base;
/
create or replace function to_dec
( p_str in varchar
p_from_base in number default
is
l_num number default
l_hex varchar
begin
for i in
l_num := l_num * p_from_base + instr(l_hex
end loop;
return l_num;
end to_dec;
/
show errors
create or replace function to_hex( p_dec in number ) return varchar
is
begin
return to_base( p_dec
end to_hex;
/
create or replace function to_bin( p_dec in number ) return varchar
is
begin
return to_base( p_dec
end to_bin;
/
create or replace function to_oct( p_dec in number ) return varchar
is
begin
return to_base( p_dec
end to_oct;
/
From:http://tw.wingwit.com/Article/program/Oracle/201311/16962.html