You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.4 KiB
75 lines
2.4 KiB
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using System.Text;
|
|
|
|
namespace MultiTerm.Protocols.Model;
|
|
|
|
/// <summary>
|
|
/// 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 <see cref="Time"/> property. E.g. to represent arrived or sent time.
|
|
/// Several methods to display the Character in other formats than Unicode are provided.
|
|
/// </summary>
|
|
public partial class ExtendedChar
|
|
{
|
|
/// <summary>
|
|
/// Data in the form of a character. UTF-16 code unit.
|
|
/// </summary>
|
|
public char Character { get; set; }
|
|
|
|
/// <summary>
|
|
/// Time that is associated with the <see cref="Character"/>.
|
|
/// E.g. to represent arrived or sent time.
|
|
/// </summary>
|
|
public TimeOnly Time { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Creates an instance of ExtendedChar with a given <paramref name="character"/>.
|
|
/// Sets <see cref="Time"/> to now.
|
|
/// </summary>
|
|
/// <param name="character">data</param>
|
|
public ExtendedChar(char character) : this(character, TimeOnly.FromDateTime(DateTime.Now)) { }
|
|
|
|
/// <summary>
|
|
/// Creates an instance of ExtendedChar with a given <paramref name="character"/> and <paramref name="time"/>.
|
|
/// Initializes string properties <see cref="StringUtf16"/>, <see cref="StringHex"/> and <see cref="StringBin"/>.
|
|
/// </summary>
|
|
/// <param name="character">data</param>
|
|
/// <param name="time">time</param>
|
|
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;
|
|
}
|
|
}
|
|
|