forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexadecimalFormatter.cs
More file actions
46 lines (38 loc) · 918 Bytes
/
HexadecimalFormatter.cs
File metadata and controls
46 lines (38 loc) · 918 Bytes
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
using System.Diagnostics.Contracts;
namespace ReClassNET.Util
{
public static class HexadecimalFormatter
{
private static readonly uint[] lookup = CreateHexLookup();
private static uint[] CreateHexLookup()
{
var result = new uint[256];
for (var i = 0; i < 256; i++)
{
var s = i.ToString("X2");
result[i] = (uint)s[0] + ((uint)s[1] << 16);
}
return result;
}
public static string ToString(byte[] data)
{
Contract.Requires(data != null);
if (data.Length == 0)
{
return string.Empty;
}
var result = new char[data.Length * 2 + data.Length - 1];
var val = lookup[data[0]];
result[0] = (char)val;
result[1] = (char)(val >> 16);
for (var i = 1; i < data.Length; i++)
{
val = lookup[data[i]];
result[3 * i - 1] = ' ';
result[3 * i] = (char)val;
result[3 * i + 1] = (char)(val >> 16);
}
return new string(result);
}
}
}