Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a byte string to a number in Oracle?

I want to convert a byte string like '1001' to the integer value 9, is there a standard function for that in Oracle ?

BTW I found a custom solution on http://www.orafaq.com/wiki/Binary

like image 915
Stef Heyenrath Avatar asked Jan 20 '23 11:01

Stef Heyenrath


2 Answers

I was trying this on Oracle using CONNECT BY to convert binary to decimal in simple SELECT statement. Finally got the desired output using below code.

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);
like image 163
Saikat Sen Avatar answered Jan 31 '23 00:01

Saikat Sen


AFAIK there's no a built in function in Oracle for that, though you can use solution provided int he link in your post

like image 26
andr Avatar answered Jan 31 '23 01:01

andr