using CommunityToolkit.Mvvm.ComponentModel; using System.Text; namespace MultiTerm.Protocols.Model; /// /// A class that represents a single and contains some additional information and methods. /// A time can be stored in combination with this using the property. E.g. to represent arrived or sent time. /// Several methods to display the in other formats are provided. /// public partial class ExtendedByte { /// /// Data in the form of a byte. /// public byte Byte { 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 with a given . /// Sets to now. /// /// data public ExtendedByte(byte dataByte) : this(dataByte, TimeOnly.FromDateTime(DateTime.Now)) { } /// /// Creates an instance of with a given and . /// /// data /// time public ExtendedByte(byte dataByte, TimeOnly time) { this.Byte = dataByte; this.Time = time; } /// /// Returns this as ASCII encoded String. /// /// public override string ToString() { return Encoding.ASCII.GetString(new byte[] { this.Byte }); } /// /// Returns displayable character as string. /// ASCII control sequences are replaced with unicode control pictures. /// /// string with single character public string ToCharacterString() { char character = (char)this.Byte; string characterString; // character is ASCII encoded and is a control sequence if (char.IsAscii(character) && character <= '\x001F') { // conver to unicode Control Picture (see https://en.wikipedia.org/wiki/Control_Pictures) characterString = ((char)('\u2400' + character)).ToString(); } // else just convert using ASCII code else if(char.IsAscii(character)) { characterString = Encoding.ASCII.GetString(new byte[] { this.Byte }); } else { characterString = "?"; } return characterString; } /// /// Returns displayable hex string. /// /// public string ToHexString() { // hexadecimal converter for byte to string static string hexConverter(byte ch) => Convert.ToHexString(new byte[] { ch }); // extract bytes from character and convert to correct format return ConvertToString(new byte[] { this.Byte }, hexConverter, 2); } /// /// Returns displayable binary string. /// /// public string ToBinaryString() { // binary converter for byte to string static string binaryConverter(byte ch) => Convert.ToString(ch, 2); // extract bytes from character and convert to correct format return ConvertToString(new byte[] { this.Byte }, binaryConverter, 8); } /// /// Converts a to a string, using the given to convert a single byte to a character. /// defines how many characters the result string should contain (the left side will be padded with zeroes). /// /// converte string private static string ConvertToString(byte[] byteArray, Converter conversionFunction, int oneByteWidth) { // foreach byte convert to binary and add to string string resultString = String.Empty; // go through whole array for (int i = 0; i < byteArray.Length; i++) { // add byte to string in correct format, padding left with zeros up to width of one byte resultString += $"{conversionFunction(byteArray[i]).PadLeft(oneByteWidth, '0')}"; // if not last byte add space separator if (i < byteArray.Length - 1) { resultString += " "; } } return resultString; } }