Click to See Complete Forum and Search --> : Binary to Decimal ?


shibha
02-28-2003, 06:55 PM
Is there a readymade function to convert binary (32 bits) number to decimal ? Or is there an easy way ? I am working on ancient oracle 7.3.2.

kanu
03-01-2003, 03:09 AM
Yes,

Here it goes

FUNCTION bin_to_dec (binin IN NUMBER) RETURN NUMBER IS
v_charpos NUMBER;
v_charval CHAR(1);
v_return NUMBER DEFAULT 0;
v_power NUMBER DEFAULT 0;
v_string VARCHAR2(2000);
BEGIN
v_string := TO_CHAR(binin);
v_charpos := LENGTH(v_string);
WHILE v_charpos > 0 LOOP
v_charval := SUBSTR(v_string,v_charpos,1);
IF v_charval BETWEEN '0' AND '1' THEN
v_return := v_return + TO_NUMBER(v_charval) * POWER(2,v_power);
ELSE
RAISE_APPLICATION_ERROR(-20621,'Invalid input');
END IF;
v_charpos := v_charpos - 1;
v_power := v_power + 1;
END LOOP;
RETURN v_return;
END bin_to_dec;

Hope this helps you

:p

jmodic
03-01-2003, 03:30 PM
You might be interested in Tom Kyte's universal conversion routines that converts any base_N to any base_N and from any base_n to DEC and to HEX specificaly. An amazingly compact and efficient piece of PL/SQL code!

http://osi.oracle.com/~tkyte/hexdec/index.html

shibha
03-04-2003, 07:01 PM
Thanks, I appreciate it.