forked from BigWinston/SRecordizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
135 lines (123 loc) · 3.86 KB
/
Util.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using HabelaLabs.Utility;
using SRecordizer;
public class Util
{
/*************************************************************************/
/// <summary>
///
/// </summary>
/// <param name="inStr"></param>
/// <returns></returns>
public static string RemoveSpaces(string inStr)
{
return inStr.Replace(" ", "");
}
/*************************************************************************/
/// <summary>
///
/// </summary>
/// <param name="test"></param>
/// <returns></returns>
public static bool CheckStringIsHexOnly(string str)
{
// For C-style hex notation (0xFF) you can use @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z"
return System.Text.RegularExpressions.Regex.IsMatch(str, @"\A\b[0-9a-fA-F]+\b\Z");
}
/*************************************************************************/
/// <summary>
///
/// </summary>
/// <param name="test"></param>
/// <returns></returns>
public static bool CheckStringIsCorrectByteLength(string str)
{
if ((str.Length % 2) == 0)
return true;
else
return false;
}
/*************************************************************************/
/// <summary>
///
/// </summary>
/// <param name="test"></param>
/// <returns></returns>
public static bool CheckValidSrecInstruction(string str)
{
string x = str.ToUpper();
if ((x == "S1") || (x == "S2") || (x == "S3") || (x == "S5") ||
(x == "S7") || (x == "S8") || (x == "S9") || (x == "S0"))
return true;
else
return false;
}
/*************************************************************************/
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="chunkSize"></param>
/// <returns></returns>
public static IEnumerable<string> Chunk(string str, int chunkSize)
{
if (str.Length < chunkSize)
{
string[] s = new string[] { str };
return s;
}
else
{
try
{
List<string> s = new List<string>();
for (int i = 0; i < str.Length; i += chunkSize)
s.Add(str.Substring(i, chunkSize));
return s;
}
catch
{
ExceptionTrap.Trap("Error reading data! Check for the correct number of characters and for Hex only inputs.");
return null;
}
}
}
/*************************************************************************/
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool OnlyHexInString(string value)
{
return System.Text.RegularExpressions.Regex.IsMatch(value, @"\A\b[0-9a-fA-F]+\b\Z");
}
/*************************************************************************/
/// <summary>
///
/// </summary>
/// <param name="raw"></param>
/// <returns></returns>
public static string GetAsciiFromBytes(string rawData)
{
if ((rawData != "") && (rawData != null))
{
List<String> chunks = (List<String>)Util.Chunk(rawData, 2);
byte[] bytes = new byte[chunks.Count];
for (int x = 0; x < bytes.Length; x++)
{
byte value = System.Convert.ToByte(chunks[x], 16);
if (value < 0x20 || value > 0x7e)
{
// Don't try to print non-printable characters and instead change them to '.'
value = (byte) '.';
}
bytes[x] = value;
}
return System.Text.Encoding.ASCII.GetString(bytes);
}
else
return "";
}
}