using System.Text; namespace MultiTerm.Protocols.Model; /// /// A class that represents a Character and contains some additional information and methods. /// The time of instanciation is represented with the property. /// 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 of instanciation of this class. /// public TimeOnly TimeOfCreation { get; private set; } public ExtendedChar() { // initialize time with now this.TimeOfCreation = TimeOnly.FromDateTime(DateTime.Now); } 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; } }