-
Binary to Decimal ?
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.
-
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
-
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
Jurij Modic
ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|