Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ByteEncoder.cs
2{
7 public class ByteEncoder : ITextEncoder
8 {
9 private readonly System.Text.Encoding iso_8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
10 private readonly BitWriter output;
11
17 public ByteEncoder(BitWriter Output)
18 {
19 this.output = Output;
20 }
21
27 public static bool CanEncode(string _)
28 {
29 return true;
30 }
31
37 public byte[] GetBytes(string Text)
38 {
39 byte[] Bin = this.iso_8859_1.GetBytes(Text);
40 if (this.iso_8859_1.GetString(Bin) != Text)
41 Bin = System.Text.Encoding.UTF8.GetBytes(Text);
42
43 return Bin;
44 }
45
50 public void Encode(byte[] Bin)
51 {
52 foreach (byte b in Bin)
53 this.output.WriteBits(b, 8);
54 }
55
60 public void Encode(string Text)
61 {
62 byte[] Bin = this.iso_8859_1.GetBytes(Text);
63 if (this.iso_8859_1.GetString(Bin) != Text)
64 Bin = System.Text.Encoding.UTF8.GetBytes(Text);
65
66 this.Encode(Bin);
67 }
68 }
69}
Writes a sequence of bits (Most significant bits first).
Definition: BitWriter.cs:10
Encodes alphanumeric strings from the ISO 8859-1 character set (by default). If that fails,...
Definition: ByteEncoder.cs:8
ByteEncoder(BitWriter Output)
Encodes alphanumeric strings from the ISO 8859-1 character set (by default). If that fails,...
Definition: ByteEncoder.cs:17
static bool CanEncode(string _)
Checks if a text string can be encoded using the alphanumeric encoding.
Definition: ByteEncoder.cs:27
void Encode(string Text)
Encodes a string.
Definition: ByteEncoder.cs:60
void Encode(byte[] Bin)
Encodes a string.
Definition: ByteEncoder.cs:50
byte[] GetBytes(string Text)
Gets the byte representation of a string.
Definition: ByteEncoder.cs:37
Interface for text encoders.
Definition: ITextEncoder.cs:7