十六进制转化为十进制:to_number()sql> select to_number('a','x') from dual; TO_NUMBER('
十六进制转化为十进制:to_number()
sql> select to_number('a','x') from dual;
TO_NUMBER('A','X')
------------------
10
十进制转化为十六进制:to_char()
SQL> select to_char(10,'xxx') from dual;
TO_CHAR(10,'
------------
a
二进制没有办法直接转化可以通过函数实现:
十进制转化为二进制
CREATE OR REPLACE FUNCTioN NUMBER_TO_BIT(V_NUM NUMBER)
RETURN VARCHAR IS V_RTN VARCHAR(8);--注意返回列长度
V_N1 NUMBER;
V_N2 NUMBER;
BEGIN
V_N1 := V_NUM;
LOOP
V_N2 := MOD(V_N1, 2);
V_N1 := ABS(TRUNC(V_N1 / 2)); --取商的绝对值
V_RTN := TO_CHAR(V_N2) || V_RTN;
EXIT WHEN V_N1 = 0;
END LOOP;
--返回二进制长度
SELECT lpad(V_RTN,8,0) --八位不足补0
INTO V_RTN
FROM dual;
return V_RTN;
end;
二进制转化为十进制:
CREATE OR REPLACE FUNCTION BIT_TO_NUMBER(P_BIN IN VARCHAR2) RETURN NUMBER
AS
V_SQL VARCHAR2(30000) := 'SELECT BIN_TO_NUM(';
V_RETURN NUMBER;
BEGIN
IF LENGTH(P_BIN) >= 256 THEN
RaiSE_APPLICATION_ERROR(-20001, 'INPUT BIN TOO LONG!');
END IF;
IF LTRIM(P_BIN, '01') IS NOT NULL THEN
RAISE_APPLICATION_ERROR(-20002, 'INPUT STR IS NOT VALID BIN
VALUE!');
END IF;
FOR I IN 1 .. LENGTH(P_BIN) LOOP
V_SQL := V_SQL || SUBSTR(P_BIN, I, 1) || ',';
END LOOP;
V_SQL := RTRIM(V_SQL, ',') || ') FROM DUAL';
EXECUTE IMMEDIATE V_SQL
INTO V_RETURN;
RETURN V_RETURN;
END;
创建函数同义词
create public synonym number_to_bit for number_to_bit;
grant execute on number_to_bit to public
十六转二和二转十六,则可以结合十转十六函数和二转十函数进行,这里不在额外举例。
ASCII码及oracle元数据转换函数:RAWTOHEX和HEXTORAW
SQL> select rawtohex(1) from dual;
RAWTOHEX(1)
------------
C102
SQL> select rawtohex('1') from dual;
RAWTOH
------
31
SQL> select rawtohex('a') from dual;
RAWTOH
------
61
SQL> select rawtohex('A') from dual;
RAWTOH
------
41
SQL> select hextoraw('A') from dual;
HE
--
0A
SQL> select hextoraw('1') from dual;
HE
--
01
--结束END--
本文标题: Oracle通过函数进行进制转化
本文链接: https://lsjlt.com/news/43694.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0