DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: Binary to Decimal ?

  1. #1
    Join Date
    Jun 2002
    Location
    Denver
    Posts
    54

    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.

  2. #2
    Join Date
    Mar 2003
    Posts
    1
    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


  3. #3
    Join Date
    Dec 2000
    Location
    Ljubljana, Slovenia
    Posts
    4,439
    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?

  4. #4
    Join Date
    Jun 2002
    Location
    Denver
    Posts
    54
    Thanks, I appreciate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width