1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
|
6 |
namespace RomCheater.Logging |
7 |
{ |
8 |
public static class Binary<T> where T : IConvertible |
9 |
{ |
10 |
public static T ToValue(string binary) |
11 |
{ |
12 |
/* NOTE: commented out types do not have a paramter for fromBase and cannot be converted to from binary or any other base*/ |
13 |
string type = (typeof(T).Name); |
14 |
object o = default(T); |
15 |
switch (type.ToLower()) |
16 |
{ |
17 |
//case "boolean": // bool |
18 |
// o = Convert.ToBoolean(binary,2); |
19 |
// break; |
20 |
//case "string": // string |
21 |
// o = binary; |
22 |
// break; |
23 |
//case "char": // char |
24 |
// o = Convert.ToChar(binary,2); |
25 |
// break; |
26 |
case "byte": // unsigned byte |
27 |
o = Convert.ToByte(binary, 2); |
28 |
break; |
29 |
case "sbyte": // signed byte |
30 |
o = Convert.ToSByte(binary, 2); |
31 |
break; |
32 |
case "int16": // short |
33 |
o = Convert.ToInt16(binary,2); |
34 |
break; |
35 |
case "uint16": // ushort |
36 |
o = Convert.ToUInt16(binary, 2); |
37 |
break; |
38 |
case "int32": // int |
39 |
o = Convert.ToInt32(binary, 2); |
40 |
break; |
41 |
case "uint32": // uint |
42 |
o = Convert.ToUInt32(binary, 2); |
43 |
break; |
44 |
case "int64": // long |
45 |
o = Convert.ToInt64(binary, 2); |
46 |
break; |
47 |
case "uint64": // ulong |
48 |
o = Convert.ToUInt64(binary, 2); |
49 |
break; |
50 |
//case "single": // float |
51 |
// o = Convert.ToSingle(binary, 2); |
52 |
// break; |
53 |
//case "double": // double |
54 |
// o = Convert.ToDouble(binary, 2); |
55 |
// break; |
56 |
//case "decimal": // decimal |
57 |
// o = Convert.ToDecimal(binary, 2); |
58 |
// break; |
59 |
default: // object |
60 |
break; |
61 |
} |
62 |
|
63 |
return (T)Convert.ChangeType(o, typeof(T)); |
64 |
} |
65 |
} |
66 |
} |