-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathUtil.cs
More file actions
90 lines (80 loc) · 2.52 KB
/
Util.cs
File metadata and controls
90 lines (80 loc) · 2.52 KB
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
using System;
using System.Collections.Generic;
using HabelaLabs.Utility;
using SRecordizer;
public class Util
{
public static string RemoveSpaces(string inStr)
{
return inStr.Replace(" ", "");
}
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");
}
public static bool CheckStringIsCorrectByteLength(string str)
{
if ((str.Length % 2) == 0)
return true;
else
return false;
}
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;
}
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;
}
}
}
public static bool OnlyHexInString(string value)
{
return System.Text.RegularExpressions.Regex.IsMatch(value, @"\A\b[0-9a-fA-F]+\b\Z");
}
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 "";
}
}