-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomBinary.cs
More file actions
102 lines (89 loc) · 2.59 KB
/
CustomBinary.cs
File metadata and controls
102 lines (89 loc) · 2.59 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quay_Code
{
class CustomBinary
{
public static string Write4Bit(char[] input)
{
Dictionary<char, string> BitKey = new Dictionary<char, string> {
{ '0', "1100" },
{ '1', "0001" },
{ '2', "0010" },
{ '3', "0011" },
{ '4', "0100" },
{ '5', "0101" },
{ '6', "0110" },
{ '7', "0111" },
{ '8', "1000" },
{ '9', "1001" }
};
StringBuilder stringBuilder = new StringBuilder();
foreach (char c in input)
{
if (BitKey.ContainsKey(c))
{
stringBuilder.Append(BitKey[c]);
}
}
return stringBuilder.ToString();
}
public static int[] Read4Bit(string[] input)
{
Dictionary<string, int> BitKeyBack = new Dictionary<string, int>
{
{ "1100", 0},
{ "0001", 1},
{ "0010", 2},
{ "0011", 3},
{ "0100", 4},
{ "0101", 5},
{ "0110", 6},
{ "0111", 7},
{ "1000", 8},
{ "1001", 9}
};
List<int> ints = new List<int>();
for(int i=0 ; i<input.Length; i++)
{
if (BitKeyBack.ContainsKey(input[i]))
{
ints.Add(BitKeyBack[input[i]]);
}
}
return ints.ToArray();
}
public static string WriteBinary(string input, int sizeMetric)
{
StringBuilder sb = new StringBuilder();
string output = "";
foreach(char c in input)
{
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
//pad binary to fill d-bits.
switch (sizeMetric)
{
case 12:
sb.Append("011011");
break;
case 18:
sb.Append("011011");
break;
case 24:
sb.Append("11");
break;
case 32:
sb.Append("1101");
break;
default:
break;
}
output = sb.ToString();
return output;
}
}
}