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.
60 lines
1.6 KiB
60 lines
1.6 KiB
using System.Text;
|
|
|
|
namespace MultiTerm.Protocols.Model;
|
|
|
|
/// <summary>
|
|
/// A class that represents a Character and contains some additional information and methods.
|
|
/// The time of instanciation is represented with the <see cref="TimeOfCreation"/> property.
|
|
/// Several methods to display the Character in other formats than Unicode are provided.
|
|
/// </summary>
|
|
public class ExtendedChar
|
|
{
|
|
/// <summary>
|
|
/// Data in the form of a character. UTF-16 code unit.
|
|
/// </summary>
|
|
public char Character { get; set; }
|
|
|
|
/// <summary>
|
|
/// Time of instanciation of this class.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|