forked from CosmosOS/Cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToHexString.cs
269 lines (248 loc) · 9.99 KB
/
ToHexString.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
namespace Cosmos.Common.Extensions
{
/// <summary>
/// Contains various helper methods to convert ints to hex represented by string.
/// Supported types:
/// <list type="bullet">
/// <item>byte.</item>
/// <item>ushort.</item>
/// <item>int.</item>
/// <item>uint.</item>
/// <item>ulong.</item>
/// </list>
/// </summary>
public static class ToHexString
{
//TODO: Can add several more overloads for other numbertypes, with and without width argument.
/// <summary>
/// Convert byte to 2 characters long hexadecimal string, padded with '0's.
/// </summary>
/// <param name="n">A byte to be converted to hexadecimal string.</param>
/// <returns>2 characters long string value, padded with '0's.</returns>
public static string ToHex(this byte n)
{
return ConvertToHex((uint)n, 2);
}
/// <summary>
/// Convert byte to hexadecimal string of a given length.
/// </summary>
/// <param name="n">A byte to be converted to hexadecimal string.</param>
/// <param name="aWidth">The number of characters in the resulting string.</param>
/// <returns>String value, right aligned and padded on the left with '0's.
/// If aWidth is less then the length of the resulting string, the resulting
/// string would not be trimmed.
/// </returns>
public static string ToHex(this byte n, int aWidth)
{
return ConvertToHex((uint)n, aWidth);
}
/// <summary>
/// Convert int to 4 characters long hexadecimal string, padded with '0's.
/// </summary>
/// <param name="n">A int to be converted to hexadecimal string.</param>
/// <returns>4 characters long string value, padded with '0's.</returns>
public static string ToHex(this int n)
{
return ConvertToHex((uint)n, 4); //TODO: this cast might throw OverflowException. Better catch it.
}
/// <summary>
/// Convert int to hexadecimal string of a given length.
/// </summary>
/// <param name="n">A int to be converted to hexadecimal string.</param>
/// <param name="aWidth">The number of characters in the resulting string.</param>
/// <returns>String value, right aligned and padded on the left with '0's.
/// If aWidth is less then the length of the resulting string, the resulting
/// string would not be trimmed.
/// </returns>
public static string ToHex(this int n, int aWidth)
{
return ConvertToHex((uint)n, aWidth);
}
/// <summary>
/// Convert 16-bit unsigned int to 4 characters long hexadecimal string, padded with '0's.
/// </summary>
/// <param name="n">A 16-bit unsigned int to be converted to hexadecimal string.</param>
/// <returns>4 characters long string value, padded with '0's.</returns>
public static string ToHex(this ushort n)
{
return ConvertToHex((uint)n, 4);
}
/// <summary>
/// Convert 16-bit unsigned int to hexadecimal string of a given length.
/// </summary>
/// <param name="n">A 16-bit unsigned int to be converted to hexadecimal string.</param>
/// <param name="aWidth">The number of characters in the resulting string.</param>
/// <returns>String value, right aligned and padded on the left with '0's.
/// If aWidth is less then the length of the resulting string, the resulting
/// string would not be trimmed.
/// </returns>
public static string ToHex(this ushort n, int aWidth)
{
return ConvertToHex((uint)n, aWidth);
}
/// <summary>
/// Convert 32-bit unsigned int to 8 characters long hexadecimal string, padded with '0's.
/// </summary>
/// <param name="aValue">A 32-bit unsigned int to be converted to hexadecimal string.</param>
/// <returns>8 characters long string value, padded with '0's.</returns>
public static string ToHex(this uint aValue)
{
return ConvertToHex(aValue, 8);
}
/// <summary>
/// Convert 32-bit unsigned int to hexadecimal string of a given length.
/// </summary>
/// <param name="aValue">A 32-bit unsigned int to be converted to hexadecimal string.</param>
/// <param name="aWidth">The number of characters in the resulting string.</param>
/// <returns>String value, right aligned and padded on the left with '0's.
/// If aWidth is less then the length of the resulting string, the resulting
/// string would not be trimmed.
/// </returns>
public static string ToHex(this uint aValue, int aWidth)
{
return ConvertToHex(aValue, aWidth);
}
/// <summary>
/// Convert 64-bit unsigned int to 16 characters long hexadecimal string, optionally padded with '0's.
/// </summary>
/// <param name="aValue">A 64-bit unsigned int to be converted to hexadecimal string.</param>
/// <param name="withPadding">Determines if a left padding should be applied</param>
/// <returns>16 characters long string value, optionally padded with '0's.</returns>
public static string ToHex(this ulong aValue, bool withPadding = true)
{
var hex = ConvertToHex(aValue);
if (withPadding)
{
return hex.PadLeft(16, '0');
}
else
{
return hex;
}
}
/// <summary>
/// Convert 64-bit unsigned int to hexadecimal string of a given length.
/// </summary>
/// <param name="aValue">A 64-bit unsigned int to be converted to hexadecimal string.</param>
/// <param name="aWidth">The number of characters in the resulting string.</param>
/// <returns>String value, right aligned and padded on the left with '0's.
/// If aWidth is less then the length of the resulting string, the resulting
/// string would not be trimmed.
/// </returns>
public static string ToHex(this ulong aValue, int aWidth)
{
return ConvertToHex(aValue).PadLeft(aWidth, '0');
}
/// <summary>
/// Get hex prefix.
/// </summary>
/// <returns>Hex prefix, as string.</returns>
private static string GetPrefix()
{
return "0x";
}
/// <summary>
/// Get hex suffix.
/// </summary>
/// <returns>Hex suffix, as string.</returns>
private static string GetSuffix()
{
return "h";
}
/// <summary>
/// Convert 32-bit unsigned int to hexadecimal string.
/// </summary>
/// <param name="num">A 32-bit unsigned int to be converted to hexadecimal string.</param>
/// <returns>String value.</returns>
private static string ConvertToHex(uint num)
{
string xHex = string.Empty;
if (num == 0)
{
xHex = "0";
}
else
{
while (num != 0)
{
xHex = DigitToHexChar((byte)(num & 0xf)) + xHex;
num = num >> 4;
}
}
return xHex;
}
/// <summary>
/// Convert 32-bit unsigned int to hexadecimal string of a given length.
/// </summary>
/// <param name="aValue">A 32-bit unsigned int to be converted to hexadecimal string.</param>
/// <param name="aWidth">The number of characters in the resulting string.</param>
/// <returns>String value, right aligned and padded on the left with '0's.
/// If aWidth is less then the length of the resulting string, the resulting
/// string would not be trimmed.
/// </returns>
private static string ConvertToHex(uint aValue, int aWidth)
{
return ConvertToHex(aValue).PadLeft(aWidth, '0'); //TODO: PadLeft might throw ArgumentOutOfRangeException. Better catch it.
}
/// <summary>
/// Convert 64-bit unsigned int to hexadecimal string.
/// </summary>
/// <param name="num">A 64-bit unsigned int to be converted to hexadecimal string.</param>
/// <returns>String value.</returns>
private static string ConvertToHex(ulong num)
{
string xHex = string.Empty;
while (num != 0)
{
//Note; char is converted to string because Cosmos crashes when adding char and string. Frode, 7.june.
xHex = DigitToHexChar((byte)(num & 0xf)) + xHex;
num = num >> 4;
}
return xHex;
}
/// <summary>
/// Convert byte to hexadecimal char.
/// </summary>
/// <param name="d">A byte to be converted to hexadecimal char.</param>
/// <returns>Char value.</returns>
public static char DigitToHexChar(byte d)
{
switch (d)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 10:
return 'A';
case 11:
return 'B';
case 12:
return 'C';
case 13:
return 'D';
case 14:
return 'E';
case 15:
return 'F';
}
return ' ';
}
}
}