161 lines
4.7 KiB
C#
161 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace ChamChat.Models
|
|
{
|
|
public static class ANSI_X328
|
|
{
|
|
private static List<AA_Pare> _AA_Pares = new List<AA_Pare>()
|
|
{
|
|
new AA_Pare("<STX>",2),
|
|
new AA_Pare("<ETX>",3),
|
|
new AA_Pare("<EOT>",4),
|
|
new AA_Pare("<ENQ>",5),
|
|
new AA_Pare("<ACK>",6),
|
|
new AA_Pare("<LF>",10),
|
|
new AA_Pare("<CR>",13),
|
|
new AA_Pare("<DLE>",16),
|
|
new AA_Pare("<NAK>",21),
|
|
new AA_Pare("<XON>",17),
|
|
new AA_Pare("<XOFF>",19),
|
|
new AA_Pare("<SP>",32),
|
|
new AA_Pare("<SPACE>",32)
|
|
};
|
|
|
|
|
|
public static string ToStringOfHexPerChar(String ANSIX328, bool Separate)
|
|
{
|
|
byte[] asciiBytes = Encoding.ASCII.GetBytes(ANSIX328);
|
|
|
|
string result = "";
|
|
foreach (byte b in asciiBytes)
|
|
{
|
|
result += b.ToHex();
|
|
if (Separate)
|
|
result += " ";
|
|
}
|
|
return result.Trim();
|
|
}
|
|
|
|
|
|
public static String ToASCII(String ANSIX328)
|
|
{
|
|
// Remove all space.
|
|
// Find Ansi code, break string at Ansi, add ascii char and reattach rest of string
|
|
|
|
String result = ANSIX328.Replace(" ", "");
|
|
|
|
foreach (AA_Pare p in _AA_Pares)
|
|
{
|
|
while (result.Contains(p.Ansi))
|
|
{
|
|
int i = result.IndexOf(p.Ansi);
|
|
string pre = result.Substring(0, i);
|
|
string post = result.Substring(i + p.Ansi.Length);
|
|
result = pre + (char)p.Ascii + post;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String ToANSI(String ASCII)
|
|
{
|
|
String result = ASCII;
|
|
|
|
foreach (AA_Pare p in _AA_Pares)
|
|
{
|
|
for (int c = 0; c < result.Length; c++)
|
|
{
|
|
if (result.ToCharArray()[c] == (char)p.Ascii)
|
|
{
|
|
string pre = result.Substring(0, c);
|
|
string post = result.Substring(c + 1);
|
|
result = pre + p.Ansi + post;
|
|
c = -1;
|
|
|
|
// Test to show ASCII as decimal
|
|
//result = pre + "<"+p.Ascii.ToString()+">" + post;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class AA_Pare
|
|
{
|
|
public AA_Pare(String Ansi, Int32 Ascii)
|
|
{
|
|
_Ansi = Ansi;
|
|
_Ascii = Ascii;
|
|
}
|
|
|
|
private int _Ascii; public Int32 Ascii { get { return _Ascii; } }
|
|
private string _Ansi; public String Ansi { get { return _Ansi; } }
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Old implementations, not in use anymore
|
|
private static string[] _Ansi = new string[13] { "<ENQ>", "<ACK>", "<DLE>", "<STX>", "<NAK>", "<EOT>", "<ETX>", "<CR>", "<LF>", "<SP>", "<XOFF>", "<XON>", "<SPACE>" };
|
|
private static int[] _Ascii = new int[13] { 5, 6, 16, 2, 21, 4, 3, 13, 10, 32, 19, 17, 32 };
|
|
public static String ToANSI(String ASCII, bool old)
|
|
{
|
|
String result = ASCII;
|
|
for (int k = 0; k < _Ascii.Length; k++)
|
|
{
|
|
for (int c = 0; c < result.Length; c++)
|
|
{
|
|
if (result.ToCharArray()[c] == (char)_Ascii[k])
|
|
{
|
|
string pre = result.Substring(0, c);
|
|
string post = result.Substring(c + 1);
|
|
result = pre + _Ansi[k] + post;
|
|
|
|
// Test to show ASCII as decimal
|
|
//result = pre + "<"+_Ascii[k].ToString()+">" + post;
|
|
|
|
c = -1;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
public static String ToASCII(String ANSIX328, bool old)
|
|
{
|
|
// Remove all space, etc.
|
|
// Find Ansi code, break string at Ansi, add ascii char and reattach rest of string
|
|
|
|
String result = ANSIX328.Replace(" ", "");
|
|
|
|
for (int k = 0; k < _Ansi.Length; k++)
|
|
{
|
|
String Ansi = _Ansi[k];
|
|
while (result.Contains(Ansi))
|
|
{
|
|
int i = result.IndexOf(Ansi);
|
|
string pre = result.Substring(0, i);
|
|
string post = result.Substring(i + Ansi.Length);
|
|
result = pre + (char)_Ascii[k] + post;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|
|
} |