Repository navigation
Expand file tree
/
Copy pathDecoder.cs
More file actions
50 lines (46 loc) · 1.58 KB
/
Copy pathDecoder.cs
File metadata and controls
50 lines (46 loc) · 1.58 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
using System;
namespace SysBot.Base
{
/// <summary>
/// Decodes a sys-botbase protocol message into raw data.
/// </summary>
public static class Decoder
{
private static bool IsNum(char c) => (uint)(c - '0') <= 9;
private static bool IsHexUpper(char c) => (uint)(c - 'A') <= 5;
private static bool IsHexLower(char c) => (uint)(c - 'a') <= 5;
public static byte[] ConvertHexByteStringToBytes(byte[] bytes)
{
var dest = new byte[bytes.Length / 2];
for (int i = 0; i < dest.Length; i++)
{
int ofs = i * 2;
var _0 = (char)bytes[ofs + 0];
var _1 = (char)bytes[ofs + 1];
dest[i] = DecodeTuple(_0, _1);
}
return dest;
}
private static byte DecodeTuple(char _0, char _1)
{
byte result;
if (IsNum(_0))
result = (byte)((_0 - '0') << 4);
else if (IsHexUpper(_0))
result = (byte)((_0 - 'A' + 10) << 4);
else if (IsHexLower(_0))
result = (byte)((_0 - 'a' + 10) << 4);
else
throw new ArgumentOutOfRangeException(nameof(_0));
if (IsNum(_1))
result |= (byte)(_1 - '0');
else if (IsHexUpper(_1))
result |= (byte)(_1 - 'A' + 10);
else if (IsHexLower(_1))
result |= (byte)(_1 - 'a' + 10);
else
throw new ArgumentOutOfRangeException(nameof(_1));
return result;
}
}
}