BaseUtil.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. namespace ClassFile
  2. {
  3. class BaseUtil
  4. {
  5. /// <summary>
  6. /// 获取LRC
  7. /// </summary>
  8. /// <param name="SQL"></param>
  9. public static string getLRC(string SENDMESSAGE)
  10. {
  11. string message = SENDMESSAGE.Trim();
  12. if (message.Length % 2 != 0)
  13. {
  14. message = message + "0";
  15. }
  16. int LRC = 0x0;
  17. for (int i = 0; i < message.Length; i = i + 2)
  18. {
  19. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  20. LRC += inside;
  21. }
  22. string _LRC = string.Format("{0:X2}", LRC);
  23. string LRCre = "";
  24. for (int i = 0; i < _LRC.Length; i++)
  25. {
  26. int index;
  27. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  28. LRCre += string.Format("{0:X}", index);
  29. }
  30. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  31. return LRCre;
  32. }
  33. public static string ASCIIToString(string ASCII)
  34. {
  35. string ReturnStr = "";
  36. ASCII = ASCII.Replace(" ", "").Replace("3A", "").Replace("0D", "").Replace("0A", "");
  37. System.Console.WriteLine(ASCII);
  38. for (int i = 0; i < ASCII.Length; i = i + 2)
  39. {
  40. if (i + 2 < ASCII.Length)
  41. {
  42. switch (ASCII.Substring(i, 2))
  43. {
  44. case "30":
  45. ReturnStr += "0";
  46. break;
  47. case "31":
  48. ReturnStr += "1";
  49. break;
  50. case "32":
  51. ReturnStr += "2";
  52. break;
  53. case "33":
  54. ReturnStr += "3";
  55. break;
  56. case "34":
  57. ReturnStr += "4";
  58. break;
  59. case "35":
  60. ReturnStr += "5";
  61. break;
  62. case "36":
  63. ReturnStr += "6";
  64. break;
  65. case "37":
  66. ReturnStr += "7";
  67. break;
  68. case "38":
  69. ReturnStr += "8";
  70. break;
  71. case "39":
  72. ReturnStr += "9";
  73. break;
  74. case "41":
  75. ReturnStr += "A";
  76. break;
  77. case "42":
  78. ReturnStr += "B";
  79. break;
  80. case "43":
  81. ReturnStr += "C";
  82. break;
  83. case "44":
  84. ReturnStr += "D";
  85. break;
  86. case "45":
  87. ReturnStr += "E";
  88. break;
  89. case "46":
  90. ReturnStr += "F";
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. }
  97. return ReturnStr;
  98. }
  99. }
  100. }