How do I convert hexadecimal to decimal (and back again) using Oracle SQL?
如何使用Oracle SQL将十六进制转换为十进制(然后返回)?
4 个解决方案
#1
33
If you are using 8.1.5 and above you can use:
如果您正在使用8.1.5或以上版本,您可以使用:
To convert from hexadecimal to decimal:
从十六进制转换为十进制:
select to_number('AA', 'xx') from dual;
To convert from decimal to hexadecimal:
将十进制转换为十六进制:
select to_char(111, 'xxxx') from dual;
#2
11
SELECT TO_NUMBER('DEADBEEF', 'XXXXXXXX')
FROM dual
---
3735928559
SELECT TO_CHAR(3735928559, 'XXXXXXXX')
FROM dual
---
DEADBEEF
#3
5
Starting in Oracle8i, the TO_CHAR and TO_NUMBER functions can handle conversions from base 10 (decimal) to base 16 (hexadecimal) and back again:
从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
源
#4
3
FTA: Oracle to Decimal :
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
#1
33
If you are using 8.1.5 and above you can use:
如果您正在使用8.1.5或以上版本,您可以使用:
To convert from hexadecimal to decimal:
从十六进制转换为十进制:
select to_number('AA', 'xx') from dual;
To convert from decimal to hexadecimal:
将十进制转换为十六进制:
select to_char(111, 'xxxx') from dual;
#2
11
SELECT TO_NUMBER('DEADBEEF', 'XXXXXXXX')
FROM dual
---
3735928559
SELECT TO_CHAR(3735928559, 'XXXXXXXX')
FROM dual
---
DEADBEEF
#3
5
Starting in Oracle8i, the TO_CHAR and TO_NUMBER functions can handle conversions from base 10 (decimal) to base 16 (hexadecimal) and back again:
从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
源
#4
3
FTA: Oracle to Decimal :
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