BaseUtil.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. if (i != _LRC.Length - 1)
  28. {
  29. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  30. }
  31. else
  32. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber) + 1;
  33. LRCre += string.Format("{0:X}", index);
  34. }
  35. return LRCre;
  36. }
  37. }
  38. }