You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 rivejä
1.4 KiB

  1. using System;
  2. using System.Buffers;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Text;
  6. namespace JT809.Protocol.JT809Extensions
  7. {
  8. public static partial class JT809BinaryExtensions
  9. {
  10. public static string ReadBCDLittle(ReadOnlySpan<byte> buf, ref int offset, int len)
  11. {
  12. StringBuilder bcdSb = new StringBuilder(len*2);
  13. for(int i = 0; i < len; i++)
  14. {
  15. bcdSb.Append(buf[offset + i].ToString("X2"));
  16. }
  17. offset = offset + len;
  18. return bcdSb.ToString();
  19. }
  20. public static int WriteBCDLittle(IMemoryOwner<byte> memoryOwner, int offset, string data,int len)
  21. {
  22. string bcdText = data == null ? "" : data;
  23. byte[] bytes = new byte[len];
  24. int startIndex = 0;
  25. int noOfZero = len * 2 - bcdText.Length;
  26. if (noOfZero > 0)
  27. {
  28. bcdText = bcdText.Insert(startIndex, new string('0', noOfZero));
  29. }
  30. int byteIndex = 0;
  31. while (startIndex < bcdText.Length && byteIndex < len)
  32. {
  33. memoryOwner.Memory.Span[startIndex + offset] = Convert.ToByte(bcdText.Substring(startIndex, 2), 16);
  34. startIndex += 2;
  35. byteIndex++;
  36. }
  37. return len;
  38. }
  39. }
  40. }