DataType.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. namespace NationalInstruments.Tdms
  3. {
  4. public class DataType
  5. {
  6. public const int Empty = 0x0000000F;
  7. public const int Void = 0x00000000;
  8. public const int Integer8 = 0x00000001;
  9. public const int Integer16 = 0x00000002;
  10. public const int Integer32 = 0x00000003;
  11. public const int Integer64 = 0x00000004;
  12. public const int UnsignedInteger8 = 0x00000005;
  13. public const int UnsignedInteger16 = 0x00000006;
  14. public const int UnsignedInteger32 = 0x00000007;
  15. public const int UnsignedInteger64 = 0x00000008;
  16. public const int SingleFloat = 0x00000009;
  17. public const int DoubleFloat = 0x0000000A;
  18. public const int ExtendedFloat = 0x0000000B;
  19. public const int SingleFloatWithUnit = 0x00000019;
  20. public const int DoubleFloatWithUnit = 0x0000001A;
  21. public const int ExtendedFloatWithUnit = 0x0000001B;
  22. public const int String = 0x00000020;
  23. public const int Boolean = 0x00000021;
  24. public const int TimeStamp = 0x00000044;
  25. public static long GetArrayLength(int dataType, long size)
  26. {
  27. return GetLength(dataType) * size;
  28. }
  29. public static int GetLength(int dataType)
  30. {
  31. switch (dataType)
  32. {
  33. case Empty: return 0;
  34. case Void: return 1;
  35. case Integer8: return 1;
  36. case Integer16: return 2;
  37. case Integer32: return 4;
  38. case Integer64: return 8;
  39. case UnsignedInteger8: return 1;
  40. case UnsignedInteger16: return 2;
  41. case UnsignedInteger32: return 4;
  42. case UnsignedInteger64: return 8;
  43. case SingleFloat:
  44. case SingleFloatWithUnit: return 4;
  45. case DoubleFloat:
  46. case DoubleFloatWithUnit: return 8;
  47. case Boolean: return 1;
  48. case TimeStamp: return 16;
  49. default: throw new ArgumentException("Cannot determine size of data type " + dataType, "dataType");
  50. }
  51. }
  52. public static Type GetClrType(int dataType)
  53. {
  54. switch (dataType)
  55. {
  56. case Empty: return typeof(object);
  57. case Void: return typeof(object);
  58. case Integer8: return typeof(sbyte);
  59. case Integer16: return typeof(short);
  60. case Integer32: return typeof(int);
  61. case Integer64: return typeof(long);
  62. case UnsignedInteger8: return typeof(byte);
  63. case UnsignedInteger16: return typeof(ushort);
  64. case UnsignedInteger32: return typeof(uint);
  65. case UnsignedInteger64: return typeof(ulong);
  66. case SingleFloat:
  67. case SingleFloatWithUnit: return typeof(float);
  68. case DoubleFloat:
  69. case DoubleFloatWithUnit: return typeof(double);
  70. case Boolean: return typeof(bool);
  71. case String: return typeof(string);
  72. case TimeStamp: return typeof(DateTime);
  73. default: throw new Exception("Unknown data type " + dataType);
  74. }
  75. }
  76. public static int GetDataType(object value)
  77. {
  78. if (value == null) return Void;
  79. else if (value.GetType() == typeof(bool)) return Boolean;
  80. else if (value.GetType() == typeof(sbyte)) return Integer8;
  81. else if (value.GetType() == typeof(Int16)) return Integer16;
  82. else if (value.GetType() == typeof(Int32)) return Integer32;
  83. else if (value.GetType() == typeof(Int64)) return Integer64;
  84. else if (value.GetType() == typeof(byte)) return UnsignedInteger8;
  85. else if (value.GetType() == typeof(UInt16)) return UnsignedInteger16;
  86. else if (value.GetType() == typeof(UInt32)) return UnsignedInteger32;
  87. else if (value.GetType() == typeof(UInt64)) return UnsignedInteger64;
  88. else if (value.GetType() == typeof(float)) return SingleFloat;
  89. else if (value.GetType() == typeof(double)) return DoubleFloat;
  90. else if (value.GetType() == typeof(string)) return String;
  91. else if (value.GetType() == typeof(DateTime)) return TimeStamp;
  92. else throw new Exception("Unknown data type " + value.GetType());
  93. }
  94. }
  95. }