-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryToDecimal.cs
More file actions
57 lines (47 loc) · 1.41 KB
/
Copy pathbinaryToDecimal.cs
File metadata and controls
57 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace binaryToDecimal
{
class Program
{
static void Main(string[] args)
{
Kata.BinaryToString("101010101");
}
public static class Kata
{
public static string BinaryToString(string binary)
{
if (binary == null)
return "";
int[] binaryArray = new int[8];
binaryArray[0] = 128;
binaryArray[1] = 64;
binaryArray[2] = 32;
binaryArray[3] = 16;
binaryArray[4] = 8;
binaryArray[5] = 4;
binaryArray[6] = 2;
binaryArray[7] = 1;
List<char> givenBinaryList = new List<char>();
string givenBinary = Convert.ToString(binary);
givenBinaryList.AddRange(givenBinary);
int i = 0;
int normalNumber = 0;
while (i < 8)
{
if (givenBinaryList[i] == '1')
{
normalNumber += binaryArray[i];
}
i++;
}
Console.WriteLine("Normal number is: " + normalNumber);
return binary.ToString();
}
}
}
}