Wednesday 28 August 2013

Decimal To Binary Converter Function using PLSQL

Hi Friends,
        Following is the code for converting decimal value into binary.
         Hope this code will help you to reduce your work.

             I have created a procedure which will help for direct conversion.

   CREATE OR REPLACE FUNCTION apps.decimal_to_binary ( ln_num      NUMBER)
   RETURN NUMBER 
   IS      
                ln_unit      NUMBER;
                ln_input    NUMBER;
                i               NUMBER  := 0;
                ln_out      NUMBER  := 0;

   BEGIN   
                ln_input := ln_num;         --User Value is stored in variable ln_input

               WHILE ln_input > 0            --Condition to check for value is greater than zero or not

                 LOOP

                       ln_unit    := MOD( ln_input, 2 );       --To get the unit digit multiple for Binary creation
                       ln_input  := TRUNC(ln_input/2);      --To get reaming amount after each binary division
                       ln_out := TO_NUMBER(ln_unit * power(10,i) + ln_out);        --To frame a Binary Number 
                       i:= i+1;

                 END LOOP;   

               RETURN(ln_out);
    END;
    /

No comments:

Post a Comment