How to convert byte string to number in Oracle?

I want to convert a byte string like "1001" to an integer value of 9, is there a standard function for Oracle?

BTW I found my own solution at http://www.orafaq.com/wiki/Binary

+3
source share
4 answers

AFAIK there is no built-in function in Oracle for this, although you can use the solution provided by int link in your post

+1
source

See here for a library from Tom Kyte for these types of conversions.

In this case, you will have:

select to_dec('1001', 2) from dual;
+1
source

Oracle CONNECT BY SELECT. , , .

WITH INPUT AS
(SELECT REVERSE('&N') AS X FROM DUAL)
SELECT SUM(TO_NUMBER(SUBSTR(X,LEVEL,1)*POWER(2,LEVEL-1))) AS OUTPUT
FROM INPUT CONNECT BY LEVEL<=LENGTH(X);
+1

" Oracle?"

There is BIN_TO_NUM that converts a vector of bits into its equivalent number.

SET SERVEROUTPUT ON;
DECLARE
  i VARCHAR2(100) := '1001';
  o INT;
BEGIN
  i:= TRIM(BOTH ',' FROM REPLACE(REPLACE(i, '1', '1,'),'0','0,')); 
  EXECUTE IMMEDIATE 'SELECT BIN_TO_NUM('|| i || ') FROM dual' INTO o;
  DBMS_OUTPUT.put_line(i || ' => ' || o);
END;
/

Result:

1,0,0,1 => 9

DBFiddle Demo

0
source

All Articles