Oracle:如何在Oracle SQL中将十六进制转换为十进制?

chhqkbe1  于 2023-11-17  发布在  Oracle
关注(0)|答案(6)|浏览(206)

如何使用Oracle SQL将十六进制转换为十进制(然后再转换回来)?

4ioopgfo

4ioopgfo1#

如果您使用的是8.1.5及以上版本,您可以用途:
从十六进制转换为十进制:

select to_number('AA', 'xx') from dual;

字符串
要从十进制转换为十六进制:

select to_char(111, 'xxxx') from dual;

2mbi3lxu

2mbi3lxu2#

SELECT  TO_NUMBER('DEADBEEF', 'XXXXXXXX')
FROM    dual

---
3735928559

SELECT  TO_CHAR(3735928559, 'XXXXXXXX')
FROM    dual
---
 DEADBEEF

字符串

qgelzfjb

qgelzfjb3#

从Oracle8i开始,TO_CHAR和TO_NUMBER函数可以处理从基数10(十进制)到基数16(十六进制)的转换,然后再返回:

SQL> select to_char(123,'XX') to_hex, 
2    to_number('7B','XX') from_hex   
3   from dual  
4  /   
TO_     FROM_HEX
------  -----------------
7B     123

字符串
source

xxe27gdn

xxe27gdn4#

FTA:Oracle to Decimal

CREATE OR REPLACE FUNCTION hex2dec (hexnum IN CHAR) RETURN NUMBER IS
  i                 NUMBER;
  digits            NUMBER;
  result            NUMBER := 0;
  current_digit     CHAR(1);
  current_digit_dec NUMBER;
BEGIN
  digits := LENGTH(hexnum);
  FOR i IN 1..digits LOOP
     current_digit := SUBSTR(hexnum, i, 1);
     IF current_digit IN ('A','B','C','D','E','F') THEN
        current_digit_dec := ASCII(current_digit) - ASCII('A') + 10;
     ELSE
        current_digit_dec := TO_NUMBER(current_digit);
     END IF;
     result := (result * 16) + current_digit_dec;
  END LOOP;
  RETURN result;
END hex2dec;
/
show errors

CREATE OR REPLACE FUNCTION num2hex (N IN NUMBER) RETURN VARCHAR2 IS
  H  VARCHAR2(64) :='';
  N2 INTEGER      := N;
BEGIN
  LOOP
     SELECT RAWTOHEX(CHR(N2))||H
     INTO   H
     FROM   dual;

     N2 := TRUNC(N2 / 256);
     EXIT WHEN N2=0;
  END LOOP;
  RETURN H;
END num2hex;
/
show errors

字符串

ctzwtxfj

ctzwtxfj5#

尽管 to_numberto_char 的“X”选项已经存在了很长时间,但它们似乎仍然没有出现在Oracle文档中。
除了十六进制之间的相互转换之外,有时还需要进行二进制甚至八进制之间的相互转换。据我所知,Oracle没有提供任何相关功能。
我在搜索Oracle是否已经添加了一个内置的方法来转换为二进制时发现了这个线程,但似乎仍然不可用
很久以前,我写了一个PL/SQL包'Radix',可以用来在各种格式之间转换。
以下是示例SQL:

select radix.to_hex(285) from dual;
select radix.to_dec('11D', 16) from dual;
select radix.to_bin(255) from dual;
select radix.to_bin(256) from dual;

字符串
产生了这些结果:

RADIX.TO_HEX(285)
----------------------------------------
11D

RADIX.TO_DEC('11D',16)
----------------------
                   285

RADIX.TO_BIN(255)
----------------------------------------
11111111

RADIX.TO_BIN(256)
----------------------------------------
100000000


代码是开源的(MIT许可证),如果有兴趣,可以在这里获得:
Oracle Radix Package

knpiaxh1

knpiaxh16#

CREATE OR REPLACE FUNCTION HEXTODEC (p_hexadecimal IN varchar2 default '0') RETURN varchar2
IS
v__power INTEGER := 0;
v__hex_number INTEGER;
v__length NUMBER;
v__place_value VARCHAR2(100);
v__multiply NUMBER;
v__decimal number := 0;
v__decimalEEEE LONG;
BEGIN
SELECT LENGTH(UPPER(p_hexadecimal))
INTO v__length
FROM dual;
FOR i IN 1..v__length
LOOP
SELECT POWER(16,v__power)
INTO v__hex_number
FROM dual;
v__power := v__power + 1;

    FOR j IN 1..v__length LOOP
        CONTINUE WHEN i > j;
      IF i = j THEN
    SELECT DECODE(SUBSTR(SUBSTR(UPPER(p_hexadecimal), -j,j),1,1),'0',0,'1',1,'2',2,'3',3,'4',4,'5',5,'6',6,'7',7,'8',8,'9',9,'A',10,'B',11,'C',12,'D',13,'E',14,'F',16 ,NULL)
    INTO v__place_value
    FROM DUAL;
    v__multiply := v__place_value * v__hex_number;        
    v__decimal := NVL(v__multiply,0)+ NVL(v__decimal,0);
    END IF;
    END LOOP;
END LOOP;
        RETURN (v__decimal);
 
EXCEPTION
WHEN OTHERS THEN
    SELECT TO_CHAR(v__decimal,'9.9EEEE')
    INTO v__decimalEEEE
    FROM DUAL;
    RETURN v__decimalEEEE;
END;
/

字符串

相关问题