十六进制转化为十进制: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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。