 # ljwoodward's solution

## to Binary in the PL/SQL Track

Published at Sep 11 2019 · 0 comments
Instructions
Test suite
Solution

Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles.

Implement binary to decimal conversion. Given a binary input string, your program should produce a decimal output. The program should handle invalid inputs.

## Note

• Implement the conversion yourself. Do not use something else to perform the conversion for you.

Decimal is a base-10 system.

A number 23 in base 10 notation can be understood as a linear combination of powers of 10:

• The rightmost digit gets multiplied by 10^0 = 1
• The next number gets multiplied by 10^1 = 10
• ...
• The nth number gets multiplied by 10^(n-1).
• All these values are summed.

So: `23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10`

Binary is similar, but uses powers of 2 rather than powers of 10.

So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`.

## Setup

Go through the setup instructions for PL/SQL to get ready to code:

http://exercism.io/languages/plsql

## Running the Tests

Execute the tests by calling the `run` method in the respective `ut_<exercise>#` package. The necessary code should be contained at the end of the test package. As an example, the test for the hamming exercise would be run using

``````begin
ut_hamming#.run;
end;
/
``````

## Source

All of Computer Science http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### ut_binary#.plsql

``````create or replace package ut_binary#
is
procedure run;
end ut_binary#;
/

create or replace package body ut_binary#
is
procedure test (
i_descn                                       varchar2
,i_exp                                         pls_integer
,i_act                                         pls_integer
)
is
begin
if i_exp = i_act then
dbms_output.put_line('SUCCESS: ' || i_descn);
else
dbms_output.put_line('FAILURE: ' || i_descn || ' - expected ' || nvl('' || i_exp, 'null') || ', but received ' || nvl('' || i_act, 'null'));
end if;
end test;

procedure run
is
begin
test(i_descn => 'test_binary_1_is_decimal_1'              , i_exp => 1   , i_act => binary#.to_decimal('1'          ));
test(i_descn => 'test_binary_10_is_decimal_2'             , i_exp => 2   , i_act => binary#.to_decimal('10'         ));
test(i_descn => 'test_binary_11_is_decimal_3'             , i_exp => 3   , i_act => binary#.to_decimal('11'         ));
test(i_descn => 'test_binary_100_is_decimal_4'            , i_exp => 4   , i_act => binary#.to_decimal('100'        ));
test(i_descn => 'test_binary_1001_is_decimal_9'           , i_exp => 9   , i_act => binary#.to_decimal('1001'       ));
test(i_descn => 'test_binary_11010_is_decimal_26'         , i_exp => 26  , i_act => binary#.to_decimal('11010'      ));
test(i_descn => 'test_binary_10001101000_is_decimal_1128' , i_exp => 1128, i_act => binary#.to_decimal('10001101000'));
test(i_descn => 'test_invalid_binary_postfix_is_decimal_0', i_exp => 0   , i_act => binary#.to_decimal('10110a'     ));
test(i_descn => 'test_invalid_binary_prefix_is_decimal_0' , i_exp => 0   , i_act => binary#.to_decimal('a10110'     ));
test(i_descn => 'test_invalid_binary_infix_is_decimal_0'  , i_exp => 0   , i_act => binary#.to_decimal('101a10'     ));
test(i_descn => 'test_invalid_binary_is_decimal_0'        , i_exp => 0   , i_act => binary#.to_decimal('101210'     ));
end run;
end ut_binary#;
/

begin
ut_binary#.run;
end;
/``````
``````CREATE OR REPLACE PACKAGE binary# AS
FUNCTION to_decimal (p_bin VARCHAR2) RETURN NUMBER;
END binary#;
/

CREATE OR REPLACE PACKAGE BODY binary# AS
FUNCTION to_decimal (p_bin NUMBER) RETURN NUMBER
IS
v_bin         NUMBER := 0;
v_total       NUMBER := 0;
v_multiplier  NUMBER := 1;
BEGIN
FOR i IN 1..length(p_bin) LOOP
CASE substr(p_bin, i * -1, 1)
WHEN '0' THEN
NULL;
WHEN '1' THEN
v_total := v_total + v_multiplier;
ELSE
RETURN 0;
END CASE;
v_multiplier := v_multiplier * 2;
END LOOP;

RETURN v_total;
END to_decimal;
END binary#;
/``````