Published at Jul 13 2018
·
1 comment

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.

- 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
*n*th 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`

.

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

http://exercism.io/languages/plsql

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;
/
```

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

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

```
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#
is
--+--------------------------------------------------------------------------+
-- Convert a binary number, represented as a string (e.g. '101010'),
-- to its decimal equivalent
--+--------------------------------------------------------------------------+
function to_decimal (
binary_string varchar2
) return pls_integer;
end binary#;
/
create or replace package body binary#
is
function to_decimal (
binary_string varchar2
) return pls_integer
as
l_binary_string varchar2(32000 char) := binary_string;
l_decimal pls_integer := 0;
l_current_char char(1);
begin
while length(l_binary_string) >= 1
loop
l_decimal := l_decimal * 2; -- moving one char in binary string means multiplication by 2
l_current_char := substr(l_binary_string,1,1);
l_binary_string := substr(l_binary_string,2);
if l_current_char = '1'
then
l_decimal := l_decimal + l_current_char; -- add current char, if '1'
elsif l_current_char != '0'
then
return 0;
end if;
end loop;
return l_decimal;
exception
when others
then raise;
end to_decimal;
end binary#;
/
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

Oh, seems I forgot to mention something: you could just user for i in reverse 1 .. length(l_binary_string) loop to save getting a substring and instead use substr(l_binary_string, i, 1).

And just on a side note on performance: you're now doing an implicit conversion between char and numeric. Adding the constant 1 should be faster (ok, that's really nitpicking now ;-)