Multiprocotol Terminalprogram (BAT)
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.
MultiTerm/MultiTerm.Protocols/Model/ExtendedChar.cs

150 lines
4.9 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.Character.ToString();
}
public string ToUtf16String()
{
string characterString;
if (char.IsWhiteSpace(this.Character))
{
characterString = this.Character switch
{
'\t' => "\\t",
' ' => " ",
'\n' => "\\n",
'\r' => "\\r",
'\v' => "\\v",
'\f' => "\\f",
_ => this.Character.ToString()
};
}
else if (char.IsControl(this.Character))
{
characterString = "<CTRL>";
}
else
{
characterString = this.Character.ToString();
}
return characterString;
}
public string ToHexString()
{
// hexadecimal converter for byte to string
Converter<byte, string> hexConverter = (ch) => Convert.ToHexString(new byte[] { ch });
// extract bytes from character and convert to correct format
return ConvertToString(GetCharacterByteArray(this.Character), hexConverter, 2);
//// foreach byte convert to hex and add to string
//string hexString = String.Empty;
//for (int i = 0; i < byteArray.Length; i++)
//{
// // add byte to string in binary format, padding left with zeros up to 8
// hexString += $"{Convert.ToString(byteArray[i], 2).PadLeft(8, '0')}";
// // if not last byte add space separator
// if (i < byteArray.Length - 1)
// {
// hexString += " ";
// }
//}
//return hexString;
//// get byte array from character and convert to hexadecimal
//return Convert.ToHexString(GetCharacterByteArray(this.Character)).PadLeft(2);
}
public string ToBinaryString()
{
// binary converter for byte to string
Converter<byte, string> binaryConverter = (ch) => Convert.ToString(ch, 2);
// extract bytes from character and convert to correct format
return ConvertToString(GetCharacterByteArray(this.Character), binaryConverter, 8);
}
private static string ConvertToString(byte[] byteArray, Converter<byte, string> 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;
}
private static byte[] GetCharacterByteArray(char character)
{
byte[] byteArray = Array.Empty<byte>();
if (char.IsAscii(character))
{
byteArray = Encoding.ASCII.GetBytes(character.ToString());
}
else
{
// extract bytes from utf16 character
byteArray = Encoding.Unicode.GetBytes(character.ToString());
}
return byteArray;
}
}