using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace PepperDash.Essentials.Core.Communications
{
///
///
///
public class ComTextHelper
{
///
/// Gets escaped text for a byte array
///
///
///
public static string GetEscapedText(byte[] bytes)
{
return string.Concat(bytes.Select(b => string.Format(@"[{0:X2}]", (int)b)).ToArray());
}
///
/// Gets escaped text for a string
///
///
///
///
/// GetEscapedText method
///
public static string GetEscapedText(string text)
{
var bytes = Encoding.GetEncoding(28591).GetBytes(text);
return string.Concat(bytes.Select(b => string.Format(@"[{0:X2}]", (int)b)).ToArray());
}
///
/// Gets debug text for a string
///
///
///
///
/// GetDebugText method
///
public static string GetDebugText(string text)
{
return Regex.Replace(text, @"[^\u0020-\u007E]", a => GetEscapedText(a.Value));
}
}
}