Util.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Edge.Core.Parser.BinaryParser.Util;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace VeederRoot_ATG_Console
  7. {
  8. public static class Util
  9. {
  10. /// <summary>
  11. /// Convert an ASCII string to a long value.
  12. /// </summary>
  13. /// <param name="hexAscIIStr">like: "A1" will return 161, "01" will return 1, "1101" will return 4353</param>
  14. /// <returns>e.g.:161</returns>
  15. public static double ConvertHexStrToLong(string hexAscIIStr)
  16. {
  17. hexAscIIStr = hexAscIIStr.Replace(" ", "").ToUpper();
  18. int iPos, iLimit;
  19. long lRetVal = 0;
  20. char chCurrent;
  21. iLimit = hexAscIIStr.Length;
  22. if ((iLimit < 1) || (iLimit > 8))
  23. return -1;
  24. for (iPos = iLimit; iPos >= 1; iPos--)
  25. {
  26. chCurrent = hexAscIIStr[iPos - 1];
  27. switch (chCurrent)
  28. {
  29. case '0':
  30. case '1':
  31. case '2':
  32. case '3':
  33. case '4':
  34. case '5':
  35. case '6':
  36. case '7':
  37. case '8':
  38. case '9':
  39. lRetVal += (chCurrent - 48) * (long)Math.Pow(16.0, (double)(iLimit - iPos));
  40. break;
  41. case 'A':
  42. case 'a':
  43. case 'B':
  44. case 'b':
  45. case 'C':
  46. case 'c':
  47. case 'D':
  48. case 'd':
  49. case 'E':
  50. case 'e':
  51. case 'F':
  52. case 'f':
  53. lRetVal += (chCurrent - 65 + 10) * (long)Math.Pow(16.0, (double)(iLimit - iPos));
  54. break;
  55. default:
  56. return -1;
  57. }
  58. }
  59. return lRetVal;
  60. }
  61. /// <summary>
  62. /// Convert bytes of ascii encoded value to its real hex value bytes.
  63. /// </summary>
  64. /// <param name="hexAscIIStr">like: 0x31, 0x33 will return byte[0] = 0x13,
  65. /// 0x30, 0x33 will return byte[0]=0x03,
  66. /// 0x34, 0x33, 0x42, 0x41 will return byte[0]=0x43, byte[1]=0xBA,
  67. /// </param>
  68. /// <returns></returns>
  69. public static byte[] ConvertHexBcdStrToBytes(byte[] hexAscIIBytes)
  70. {
  71. if (hexAscIIBytes.Length % 2 != 0)
  72. throw new ArgumentException("Input hexStr length must be even, but now is: " + hexAscIIBytes.Length);
  73. var hexStr = Encoding.ASCII.GetString(hexAscIIBytes);
  74. return hexStr.ToBytes();
  75. }
  76. /// <summary>
  77. ///
  78. /// </summary>
  79. /// <param name="raw">must be length of 4</param>
  80. /// <returns>with 6 fractional digits</returns>
  81. public static double ConvertIEEEWith4BytesToDouble(byte[] raw)
  82. {
  83. if (raw == null || raw.Length != 4)
  84. throw new ArgumentException("The target for convert from IEEE to float must be length 4");
  85. var r = BitConverter.ToSingle(raw.Reverse().ToArray());
  86. return r;
  87. //0 is positive, 1 is negative.
  88. //byte signBit = raw[0].GetBit(0);
  89. //return 0;
  90. }
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. /// <param name="raw">must be length of 4</param>
  95. /// <returns>with 6 fractional digits</returns>
  96. public static double ConvertIEEEWith8BytesToDouble(byte[] raw)
  97. {
  98. if (raw == null || raw.Length != 8)
  99. throw new ArgumentException("The target for convert from IEEE to double must be length 8");
  100. var r = BitConverter.ToDouble(raw.Reverse().ToArray());
  101. return r;
  102. //0 is positive, 1 is negative.
  103. //byte signBit = raw[0].GetBit(0);
  104. //return 0;
  105. }
  106. /// <summary>
  107. ///
  108. /// </summary>
  109. /// <param name="raw"></param>
  110. /// <param name="roundTo">rounding to keep how many decimal points</param>
  111. /// <returns></returns>
  112. public static double ConvertIEEEWith4BytesToDouble(byte[] raw, int roundTo)
  113. {
  114. if (raw == null || raw.Length != 4)
  115. throw new ArgumentException("The target for convert from IEEE to float must be length 4");
  116. var r = Math.Round(BitConverter.ToSingle(raw.Reverse().ToArray()), roundTo);
  117. return r;
  118. //0 is positive, 1 is negative.
  119. //byte signBit = raw[0].GetBit(0);
  120. //return 0;
  121. }
  122. public static byte[] ConvertDoubleToIEEE4Bytes(double value)
  123. {
  124. var r = BitConverter.GetBytes((float)value);
  125. return r.Reverse().ToArray();
  126. }
  127. public static byte[] ConvertDoubleToIEEE8Bytes(double value)
  128. {
  129. var r = BitConverter.GetBytes(value);
  130. return r.Reverse().ToArray();
  131. }
  132. }
  133. }