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.
137 lines
4.7 KiB
137 lines
4.7 KiB
using System.Text;
|
|
|
|
namespace MultiTerm.Protocols.Model;
|
|
|
|
/// <summary>
|
|
/// A class that represents a single <see cref="byte"/> and contains some additional information and methods.
|
|
/// A time can be stored in combination with this <see cref="byte"/> using the <see cref="Time"/> property. E.g. to represent arrived or sent time.
|
|
/// Several methods to display the <see cref="byte"/> in other formats are provided.
|
|
/// </summary>
|
|
public partial class ExtendedByte : ICloneable
|
|
{
|
|
/// <summary>
|
|
/// Data in the form of a byte.
|
|
/// </summary>
|
|
public byte Byte { get; set; }
|
|
|
|
/// <summary>
|
|
/// Time that is associated with the <see cref="Byte"/>.
|
|
/// E.g. to represent arrived or sent time.
|
|
/// </summary>
|
|
public TimeOnly Time { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Creates an instance of <see cref="ExtendedByte"/> with a given <paramref name="dataByte"/>.
|
|
/// Sets <see cref="Time"/> to now.
|
|
/// </summary>
|
|
/// <param name="dataByte">data</param>
|
|
public ExtendedByte(byte dataByte) : this(dataByte, TimeOnly.FromDateTime(DateTime.Now)) { }
|
|
|
|
/// <summary>
|
|
/// Creates an instance of <see cref="ExtendedByte"/> with a given <paramref name="dataByte"/> and <paramref name="time"/>.
|
|
/// </summary>
|
|
/// <param name="dataByte">data</param>
|
|
/// <param name="time">time</param>
|
|
public ExtendedByte(byte dataByte, TimeOnly time)
|
|
{
|
|
this.Byte = dataByte;
|
|
this.Time = time;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns this <see cref="Byte"/> as ASCII encoded String.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return Encoding.ASCII.GetString(new byte[] { this.Byte });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns displayable character as string.
|
|
/// ASCII control sequences are replaced with unicode control pictures.
|
|
/// </summary>
|
|
/// <returns>string with single character</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns displayable hex string.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns displayable binary string.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a <paramref name="byteArray"/> to a string, using the given <paramref name="conversionFunction"/> to convert a single byte to a character.
|
|
/// <paramref name="oneByteWidth"/> defines how many characters the result string should contain (the left side will be padded with zeroes).
|
|
/// </summary>
|
|
/// <returns>converte string</returns>
|
|
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;
|
|
}
|
|
|
|
#region ICloneable
|
|
public object Clone()
|
|
{
|
|
return this.MemberwiseClone();
|
|
}
|
|
#endregion
|
|
}
|
|
|