using System.Text; namespace MultiTerm.Protocols.Model; /// /// A class that represents a Character and contains some additional information and methods. /// A time can be stored in combination with this Character using the property. E.g. to represent arrived or sent time. /// Several methods to display the Character in other formats than Unicode are provided. /// public class ExtendedChar { /// /// Data in the form of a character. UTF-16 code unit. /// public char Character { get; set; } /// /// Time that is associated with the . /// E.g. to represent arrived or sent time. /// public TimeOnly Time { get; private set; } /// /// Creates an instance of ExtendedChar with a given . /// Sets to now. /// /// data public ExtendedChar(char character) { // initialize time with now this.Time = TimeOnly.FromDateTime(DateTime.Now); this.Character = character; } /// /// Creates an instance of ExtendedChar with a given and . /// /// data /// time public ExtendedChar(char character, TimeOnly time) { this.Character = character; this.Time = time; } public override string ToString() { return this.ToUtf16String(); } public string ToUtf16String() { return this.Character.ToString(); } public string ToHexString() { // extract bytes from utf16 character byte[] byteArray = Encoding.Unicode.GetBytes(this.ToUtf16String()); // convert to hexadecimal return Convert.ToHexString(byteArray); } public string ToBinaryString() { // extract bytes from utf16 character byte[] byteArray = Encoding.Unicode.GetBytes(this.ToUtf16String()); // foreach byte convert to binary and add to string string binaryString = String.Empty; foreach (byte b in byteArray) { binaryString += $"{Convert.ToString(b, 2)} "; } return binaryString; } }