-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversions.cs
236 lines (204 loc) · 7.91 KB
/
Conversions.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
using System;
using System.Globalization;
namespace Rampastring.Tools
{
/// <summary>
/// Provides static methods for converting data types.
/// </summary>
public static class Conversions
{
/// <summary>
/// Converts a string to a boolean.
/// </summary>
/// <param name="str">The string to convert.</param>
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
/// <returns>A boolean based on the given string.</returns>
public static bool BooleanFromString(string str, bool defaultValue)
{
if (string.IsNullOrEmpty(str))
return defaultValue;
char firstChar = str.ToLower(CultureInfo.InvariantCulture)[0];
switch (firstChar)
{
case 't':
case 'y':
case '1':
case 'a':
case 'e':
return true;
case 'n':
case 'f':
case '0':
return false;
default:
return defaultValue;
}
}
/// <summary>
/// Converts a boolean to a string with the specified style.
/// </summary>
/// <param name="boolean">The boolean.</param>
/// <param name="stringStyle">The style of the boolean string.</param>
/// <returns>A string that represents the boolean with the specified style.</returns>
public static string BooleanToString(bool boolean, BooleanStringStyle stringStyle)
{
string trueString;
string falseString;
switch (stringStyle)
{
case BooleanStringStyle.TRUEFALSE_LOWERCASE:
trueString = "true";
falseString = "false";
break;
case BooleanStringStyle.YESNO:
trueString = "Yes";
falseString = "No";
break;
case BooleanStringStyle.YESNO_LOWERCASE:
trueString = "yes";
falseString = "no";
break;
case BooleanStringStyle.ONEZERO:
trueString = "1";
falseString = "0";
break;
default:
case BooleanStringStyle.TRUEFALSE:
trueString = "True";
falseString = "False";
break;
}
return boolean ? trueString : falseString;
}
/// <summary>
/// Converts a string with the English number format to a float.
/// </summary>
/// <param name="str">The string to convert.</param>
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
/// <returns>A float based on the given string.</returns>
public static float FloatFromString(string str, float defaultValue)
{
if (string.IsNullOrEmpty(str))
return defaultValue;
try
{
return Convert.ToSingle(str, CultureInfo.GetCultureInfo("en-US").NumberFormat);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// Converts a string with the English number format to a double.
/// </summary>
/// <param name="str">The string to convert.</param>
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
/// <returns>A double based on the given string.</returns>
public static double DoubleFromString(string str, double defaultValue)
{
if (string.IsNullOrEmpty(str))
return defaultValue;
try
{
return Convert.ToDouble(str, CultureInfo.GetCultureInfo("en-US").NumberFormat);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// Converts a string with the English number format to an integer.
/// </summary>
/// <param name="str">The string to convert.</param>
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
/// <returns>An integer based on the given string.</returns>
public static int IntFromString(string str, int defaultValue)
{
// In theory the "if" here is useless, but having it here
// makes the code run 100+ times faster for null / empty strings.
if (string.IsNullOrEmpty(str))
return defaultValue;
try
{
return int.Parse(str, CultureInfo.InvariantCulture);
}
catch
{
return defaultValue;
}
}
public static int[] IntArrayFromStringArray(string[] array)
{
int[] intArray = new int[array.Length];
for (int i = 0; i < array.Length; i++)
intArray[i] = int.Parse(array[i], CultureInfo.InvariantCulture);
return intArray;
}
/// <summary>
/// Converts an array of booleans into an array of bytes,
/// packing 8 boolean values into a single byte.
/// </summary>
/// <param name="boolArray">The boolean array.</param>
/// <returns>The generated array of bytes.</returns>
public static byte[] BoolArrayIntoBytes(bool[] boolArray)
{
// Slight modification of Marc Gravell's code at
// http://stackoverflow.com/questions/713057/convert-bool-to-byte
int byteCount = boolArray.Length / 8;
if ((boolArray.Length % 8) != 0)
byteCount++;
byte[] bytes = new byte[byteCount];
int optionIndex = 0;
int byteIndex = 0;
for (int i = 0; i < boolArray.Length; i++)
{
if (boolArray[i])
{
bytes[byteIndex] |= (byte)(((byte)1) << optionIndex);
}
optionIndex++;
if (optionIndex == 8)
{
optionIndex = 0;
byteIndex++;
}
}
return bytes;
}
public static bool[] BytesIntoBoolArray(byte[] byteArray)
{
int booleanCount = byteArray.Length * 8;
bool[] boolArray = new bool[booleanCount];
// Worth reading:
// http://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work
for (int i = 0; i < byteArray.Length; i++)
{
byte b = byteArray[i];
bool[] booleans = ByteToBoolArray(b);
for (int j = 0; j < booleans.Length; j++)
{
boolArray[i * 8 + j] = booleans[j];
}
}
return boolArray;
}
/// <summary>
/// Converts a byte to an array of 8 booleans.
/// </summary>
/// <param name="b">The byte.</param>
/// <returns>An array of 8 booleans.</returns>
public static bool[] ByteToBoolArray(byte b)
{
// prepare the return result
bool[] result = new bool[8];
// check each bit in the byte. if 1 set to true, if 0 set to false
for (int i = 0; i < 8; i++)
result[i] = (b & (1 << i)) == 0 ? false : true;
// reverse the array
// Array.Reverse(result);
return result;
}
}
}