Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NumericEncoder.cs
2{
7 {
8 private readonly BitWriter output;
9
15 {
16 this.output = Output;
17 }
18
24 public static bool CanEncode(string Text)
25 {
26 foreach (char ch in Text)
27 {
28 if (ch < '0' || ch > '9')
29 return false;
30 }
31
32 return true;
33 }
34
39 public void Encode(string Text)
40 {
41 int i = 0;
42 int c = 0;
43
44 foreach (char ch in Text)
45 {
46 if (ch < '0' || ch > '9')
47 continue;
48
49 i *= 10;
50 i += ch - '0';
51
52 if (++c >= 3)
53 {
54 this.output.WriteBits((uint)i, 10);
55 i = c = 0;
56 }
57 }
58
59 switch (c)
60 {
61 case 1:
62 this.output.WriteBits((uint)i, 4);
63 break;
64
65 case 2:
66 this.output.WriteBits((uint)i, 7);
67 break;
68 }
69 }
70
77 public static int GetByteLength(int NrCharacters)
78 {
79 int NrBits = 10 * (NrCharacters / 3);
80
81 switch ((NrCharacters & 3))
82 {
83 case 1:
84 NrBits += 4;
85 break;
86
87 case 2:
88 NrBits += 7;
89 break;
90 }
91
92 return (NrBits + 7) / 8;
93 }
94
95 }
96}
Writes a sequence of bits (Most significant bits first).
Definition: BitWriter.cs:10
NumericEncoder(BitWriter Output)
Encodes numeric strings
static int GetByteLength(int NrCharacters)
Gets the number of bytes required to encode an alphanumeric message containing a specific number of c...
static bool CanEncode(string Text)
Checks if a text string can be encoded using the alphanumeric encoding.
void Encode(string Text)
Encodes a string.
Interface for text encoders.
Definition: ITextEncoder.cs:7