namespace ConsoleApplication3 class Program static void Main string ar

 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
namespace ConsoleApplication3 {
class Program {
static void Main(string[] args) {
int n1 = StringToInt("1");
int n2 = StringToInt("01");
int n3 = StringToInt("11");
int n4 = StringToInt("10110110");
bool[] b0 = IntToBool(0);
bool[] b1 = IntToBool(1);
bool[] b2 = IntToBool(2);
bool[] b3 = IntToBool(3);
bool[] b4 = IntToBool(4);
bool[] b5 = IntToBool(5);
bool[] b6 = IntToBool(6);
bool[] b7 = IntToBool(7);
bool[] b8 = IntToBool(8);
bool[] b9 = IntToBool(9);
bool[] b10 = IntToBool(10);
bool[] b11 = IntToBool(11);
bool[] b12 = IntToBool(12);
bool[] b13 = IntToBool(13);
bool[] b14 = IntToBool(14);
bool[] b15 = IntToBool(15);
}
static string BoolToString(bool[] bools) {
return string.Join("", bools.Select(b => b ? "1": "0"));
}
static bool[] StringToBool(string str) {
return str.Select(c => c == '1' ? true : false).ToArray<bool>();
}
static int StringToInt(string str) {
return BoolToInt(StringToBool(str));
}
static int BoolToInt(bool[] bools) {
int result = 0;
for(int i = 0; i < bools.Length; i++) {
result += (bools[i] ? 1 : 0) * (int)Math.Pow(2, bools.Length - (i + 1));
}
return result;
}
static bool[] IntToBool(int number) {
bool[] bools = new bool[4];
for(int i = bools.Length - 1; i >= 0; i--) {
bools[i] = number % 2 == 1 ? true : false;
number /= 2;
}
return bools;
}
}
}