using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace PepperDash.Core { /// /// Helper class for formatting communication text and byte data for debugging purposes. /// public class ComTextHelper { /// /// Gets escaped text for a byte array /// /// /// string with all bytes escaped 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 /// /// /// string with all bytes escaped 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 /// /// /// string with all non-printable characters escaped public static string GetDebugText(string text) { return Regex.Replace(text, @"[^\u0020-\u007E]", a => GetEscapedText(a.Value)); } } }