-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHex.cs
120 lines (104 loc) · 4.37 KB
/
Hex.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
using System;
using System.Collections.Generic;
using System.Text;
namespace Romulus
{
public static class Hex
{
static char[] hexDigitsU = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
static char[] hexDigitsL = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static string FormatHex(byte[] hex, HexCasing casing) {
if (hex == null) throw new ArgumentNullException("hex");
StringBuilder result = new StringBuilder(hex.Length * 2);
FormatHex(hex, result, casing);
return result.ToString();
}
public static string FormatHex(byte[] hex) {
return FormatHex(hex, HexCasing.Upper);
}
public static void FormatHex(byte[] hex, StringBuilder output) {
FormatHex(hex, output, HexCasing.Upper);
}
public static void FormatHex(byte[] hex, StringBuilder output, HexCasing casing) {
if (hex == null) throw new ArgumentNullException("hex");
char[] digits;
if (casing == HexCasing.Upper)
digits = hexDigitsU;
else if (casing == HexCasing.Lower)
digits = hexDigitsL;
else
throw new ArgumentException("Invalid value for casing.", "casing");
for (int i = 0; i < hex.Length; i++) {
int hexVal = hex[i];
int highNib = hexVal >> 4;
int lowNib = hexVal & 0xF;
output.Append(digits[highNib]);
output.Append(digits[lowNib]);
}
}
/// <summary>
/// Returns the integer value of the specified hex digit, or -1 if the specified character is not a valid hex digit.
/// </summary>
/// <param name="d">The hex digit to parse.</param>
/// <returns>An integer between -1 and 15.</returns>
public static int ParseDigit(char d) {
if (d >= '0' & d <= '9') {
return (int)(d - '0');
}
if (d >= 'A' & d <= 'F') {
return (int)(d - 'A' + 10);
}
if (d >= 'a' & d <= 'f') {
return (int)(d - 'a' + 10);
}
return -1;
}
public static byte[] ParseHex(string hex) {
if (hex == null)
throw new ArgumentNullException("hex");
// Require even number of bytes
if (hex.Length % 2 == 1)
throw new ArgumentException("Parameter \"hex\" is not a valid hex string.", "hex");
byte[] result = new byte[hex.Length / 2];
int iChar = 0;
for (int iByte = 0; iByte < result.Length; iByte++) {
int byteValue = 0;
// Parse first digit of byte
char digit = hex[iChar];
if (digit >= '0' & digit <= '9') {
byteValue |= (int)(digit - '0');
} else if (digit >= 'A' & digit <= 'F') {
byteValue |= (int)(digit - ('A' - (char)10));
} else if (digit >= 'a' & digit <= 'f') {
byteValue |= (int)(digit - ('a' - (char)10));
} else {
throw new ArgumentException("Parameter \"hex\" is not a valid hex string.", "hex");
}
// Move to high nibble
byteValue <<= 4;
// Next char
iChar++;
// Parse second digit of byte
digit = hex[iChar];
if (digit >= '0' & digit <= '9') {
byteValue |= (int)(digit - '0');
} else if (digit >= 'A' & digit <= 'F') {
byteValue |= (int)(digit - ('A' - (char)10));
} else if (digit >= 'a' & digit <= 'f') {
byteValue |= (int)(digit - ('a' - (char)10));
} else {
throw new ArgumentException("Parameter \"hex\" is not a valid hex string.", "hex");
}
// Next char
iChar++;
result[iByte] = (byte)byteValue;
}
return result;
}
}
public enum HexCasing
{
Upper,
Lower
}
}