|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | + |
| 4 | +namespace Rampastring.Tools |
| 5 | +{ |
| 6 | + |
| 7 | + /// <summary> |
| 8 | + /// Provides static methods for converting data types. |
| 9 | + /// </summary> |
| 10 | + public static class Conversions |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Converts a string to a boolean. |
| 14 | + /// </summary> |
| 15 | + /// <param name="str">The string to convert.</param> |
| 16 | + /// <param name="defaultValue">The default value to return if the conversion fails.</param> |
| 17 | + /// <returns>A boolean based on the given string.</returns> |
| 18 | + public static bool BooleanFromString(string str, bool defaultValue) |
| 19 | + { |
| 20 | + if (string.IsNullOrEmpty(str)) |
| 21 | + return defaultValue; |
| 22 | + |
| 23 | + char firstChar = str.ToLower(CultureInfo.InvariantCulture)[0]; |
| 24 | + |
| 25 | + switch (firstChar) |
| 26 | + { |
| 27 | + case 't': |
| 28 | + case 'y': |
| 29 | + case '1': |
| 30 | + case 'a': |
| 31 | + case 'e': |
| 32 | + return true; |
| 33 | + case 'n': |
| 34 | + case 'f': |
| 35 | + case '0': |
| 36 | + return false; |
| 37 | + default: |
| 38 | + return defaultValue; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Converts a boolean to a string with the specified style. |
| 44 | + /// </summary> |
| 45 | + /// <param name="boolean">The boolean.</param> |
| 46 | + /// <param name="stringStyle">The style of the boolean string.</param> |
| 47 | + /// <returns>A string that represents the boolean with the specified style.</returns> |
| 48 | + public static string BooleanToString(bool boolean, BooleanStringStyle stringStyle) |
| 49 | + { |
| 50 | + string trueString; |
| 51 | + string falseString; |
| 52 | + |
| 53 | + switch (stringStyle) |
| 54 | + { |
| 55 | + case BooleanStringStyle.TRUEFALSE_LOWERCASE: |
| 56 | + trueString = "true"; |
| 57 | + falseString = "false"; |
| 58 | + break; |
| 59 | + case BooleanStringStyle.YESNO: |
| 60 | + trueString = "Yes"; |
| 61 | + falseString = "No"; |
| 62 | + break; |
| 63 | + case BooleanStringStyle.YESNO_LOWERCASE: |
| 64 | + trueString = "yes"; |
| 65 | + falseString = "no"; |
| 66 | + break; |
| 67 | + case BooleanStringStyle.ONEZERO: |
| 68 | + trueString = "1"; |
| 69 | + falseString = "0"; |
| 70 | + break; |
| 71 | + default: |
| 72 | + case BooleanStringStyle.TRUEFALSE: |
| 73 | + trueString = "True"; |
| 74 | + falseString = "False"; |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + return boolean ? trueString : falseString; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Converts a string with the English number format to a float. |
| 83 | + /// </summary> |
| 84 | + /// <param name="str">The string to convert.</param> |
| 85 | + /// <param name="defaultValue">The default value to return if the conversion fails.</param> |
| 86 | + /// <returns>A float based on the given string.</returns> |
| 87 | + public static float FloatFromString(string str, float defaultValue) |
| 88 | + { |
| 89 | + if (string.IsNullOrEmpty(str)) |
| 90 | + return defaultValue; |
| 91 | + |
| 92 | + try |
| 93 | + { |
| 94 | + return Convert.ToSingle(str, CultureInfo.GetCultureInfo("en-US").NumberFormat); |
| 95 | + } |
| 96 | + catch |
| 97 | + { |
| 98 | + return defaultValue; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Converts a string with the English number format to a double. |
| 104 | + /// </summary> |
| 105 | + /// <param name="str">The string to convert.</param> |
| 106 | + /// <param name="defaultValue">The default value to return if the conversion fails.</param> |
| 107 | + /// <returns>A double based on the given string.</returns> |
| 108 | + public static double DoubleFromString(string str, double defaultValue) |
| 109 | + { |
| 110 | + if (string.IsNullOrEmpty(str)) |
| 111 | + return defaultValue; |
| 112 | + |
| 113 | + try |
| 114 | + { |
| 115 | + return Convert.ToDouble(str, CultureInfo.GetCultureInfo("en-US").NumberFormat); |
| 116 | + } |
| 117 | + catch |
| 118 | + { |
| 119 | + return defaultValue; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Converts a string with the English number format to an integer. |
| 125 | + /// </summary> |
| 126 | + /// <param name="str">The string to convert.</param> |
| 127 | + /// <param name="defaultValue">The default value to return if the conversion fails.</param> |
| 128 | + /// <returns>An integer based on the given string.</returns> |
| 129 | + public static int IntFromString(string str, int defaultValue) |
| 130 | + { |
| 131 | + // In theory the "if" here is useless, but having it here |
| 132 | + // makes the code run 100+ times faster for null / empty strings. |
| 133 | + if (string.IsNullOrEmpty(str)) |
| 134 | + return defaultValue; |
| 135 | + |
| 136 | + try |
| 137 | + { |
| 138 | + return int.Parse(str, CultureInfo.InvariantCulture); |
| 139 | + } |
| 140 | + catch |
| 141 | + { |
| 142 | + return defaultValue; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public static int[] IntArrayFromStringArray(string[] array) |
| 147 | + { |
| 148 | + int[] intArray = new int[array.Length]; |
| 149 | + for (int i = 0; i < array.Length; i++) |
| 150 | + intArray[i] = int.Parse(array[i], CultureInfo.InvariantCulture); |
| 151 | + |
| 152 | + return intArray; |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Converts an array of booleans into an array of bytes, |
| 157 | + /// packing 8 boolean values into a single byte. |
| 158 | + /// </summary> |
| 159 | + /// <param name="boolArray">The boolean array.</param> |
| 160 | + /// <returns>The generated array of bytes.</returns> |
| 161 | + public static byte[] BoolArrayIntoBytes(bool[] boolArray) |
| 162 | + { |
| 163 | + // Slight modification of Marc Gravell's code at |
| 164 | + // http://stackoverflow.com/questions/713057/convert-bool-to-byte |
| 165 | + |
| 166 | + int byteCount = boolArray.Length / 8; |
| 167 | + if ((boolArray.Length % 8) != 0) |
| 168 | + byteCount++; |
| 169 | + |
| 170 | + byte[] bytes = new byte[byteCount]; |
| 171 | + int optionIndex = 0; |
| 172 | + int byteIndex = 0; |
| 173 | + |
| 174 | + for (int i = 0; i < boolArray.Length; i++) |
| 175 | + { |
| 176 | + if (boolArray[i]) |
| 177 | + { |
| 178 | + bytes[byteIndex] |= (byte)(((byte)1) << optionIndex); |
| 179 | + } |
| 180 | + |
| 181 | + optionIndex++; |
| 182 | + |
| 183 | + if (optionIndex == 8) |
| 184 | + { |
| 185 | + optionIndex = 0; |
| 186 | + byteIndex++; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return bytes; |
| 191 | + } |
| 192 | + |
| 193 | + public static bool[] BytesIntoBoolArray(byte[] byteArray) |
| 194 | + { |
| 195 | + int booleanCount = byteArray.Length * 8; |
| 196 | + bool[] boolArray = new bool[booleanCount]; |
| 197 | + |
| 198 | + // Worth reading: |
| 199 | + // http://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work |
| 200 | + |
| 201 | + for (int i = 0; i < byteArray.Length; i++) |
| 202 | + { |
| 203 | + byte b = byteArray[i]; |
| 204 | + bool[] booleans = ByteToBoolArray(b); |
| 205 | + |
| 206 | + for (int j = 0; j < booleans.Length; j++) |
| 207 | + { |
| 208 | + boolArray[i * 8 + j] = booleans[j]; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + return boolArray; |
| 213 | + } |
| 214 | + |
| 215 | + /// <summary> |
| 216 | + /// Converts a byte to an array of 8 booleans. |
| 217 | + /// </summary> |
| 218 | + /// <param name="b">The byte.</param> |
| 219 | + /// <returns>An array of 8 booleans.</returns> |
| 220 | + public static bool[] ByteToBoolArray(byte b) |
| 221 | + { |
| 222 | + // prepare the return result |
| 223 | + bool[] result = new bool[8]; |
| 224 | + |
| 225 | + // check each bit in the byte. if 1 set to true, if 0 set to false |
| 226 | + for (int i = 0; i < 8; i++) |
| 227 | + result[i] = (b & (1 << i)) == 0 ? false : true; |
| 228 | + |
| 229 | + // reverse the array |
| 230 | + // Array.Reverse(result); |
| 231 | + |
| 232 | + return result; |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | +} |
0 commit comments