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.

118 lines
3.9 KiB

  1. using System;
  2. using System.Buffers;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace JT809.Protocol.Extensions
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public static partial class JT809BinaryExtensions
  12. {
  13. public static string ToHexString(this byte[] source,string separator="")
  14. {
  15. return string.Join(separator, source.Select(s => s.ToString("X2")));
  16. }
  17. public static int WriteHexStringLittle(byte[] bytes, int offset, string data, int len)
  18. {
  19. if (data == null) data = "";
  20. data = data.Replace(" ", "");
  21. int startIndex = 0;
  22. if (data.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
  23. {
  24. startIndex = 2;
  25. }
  26. int length = len;
  27. if (length == -1)
  28. {
  29. length = (data.Length - startIndex) / 2;
  30. }
  31. int noOfZero = length * 2 + startIndex - data.Length;
  32. if (noOfZero > 0)
  33. {
  34. data = data.Insert(startIndex, new string('0', noOfZero));
  35. }
  36. int byteIndex = 0;
  37. while (startIndex < data.Length && byteIndex < length)
  38. {
  39. bytes[offset+byteIndex] = Convert.ToByte(data.Substring(startIndex, 2), 16);
  40. startIndex += 2;
  41. byteIndex++;
  42. }
  43. return length;
  44. }
  45. /// <summary>
  46. /// 16进制字符串转16进制数组
  47. /// </summary>
  48. /// <param name="hexString"></param>
  49. /// <returns></returns>
  50. public static byte[] ToHexBytes(this string hexString)
  51. {
  52. hexString = hexString.Replace(" ", "");
  53. byte[] buf = new byte[hexString.Length / 2];
  54. ReadOnlySpan<char> readOnlySpan = hexString.AsSpan();
  55. for (int i = 0; i < hexString.Length; i++)
  56. {
  57. if (i % 2 == 0)
  58. {
  59. buf[i / 2] = Convert.ToByte(readOnlySpan.Slice(i, 2).ToString(), 16);
  60. }
  61. }
  62. return buf;
  63. }
  64. public static string ReadNumber(this byte value, string format = "X2")
  65. {
  66. return value.ToString(format);
  67. }
  68. public static string ReadNumber(this int value, string format = "X8")
  69. {
  70. return value.ToString(format);
  71. }
  72. public static string ReadNumber(this uint value, string format = "X8")
  73. {
  74. return value.ToString(format);
  75. }
  76. public static string ReadNumber(this long value, string format = "X16")
  77. {
  78. return value.ToString(format);
  79. }
  80. public static string ReadNumber(this ulong value, string format = "X16")
  81. {
  82. return value.ToString(format);
  83. }
  84. public static string ReadNumber(this short value, string format = "X4")
  85. {
  86. return value.ToString(format);
  87. }
  88. public static string ReadNumber(this ushort value, string format = "X4")
  89. {
  90. return value.ToString(format);
  91. }
  92. public static ReadOnlySpan<char> ReadBinary(this ushort value)
  93. {
  94. return System.Convert.ToString(value, 2).PadLeft(16, '0').AsSpan();
  95. }
  96. public static ReadOnlySpan<char> ReadBinary(this short value)
  97. {
  98. return System.Convert.ToString(value, 2).PadLeft(16, '0').AsSpan();
  99. }
  100. public static ReadOnlySpan<char> ReadBinary(this uint value)
  101. {
  102. return System.Convert.ToString(value, 2).PadLeft(32, '0').AsSpan();
  103. }
  104. public static ReadOnlySpan<char> ReadBinary(this int value)
  105. {
  106. return System.Convert.ToString(value, 2).PadLeft(32, '0').AsSpan();
  107. }
  108. public static ReadOnlySpan<char> ReadBinary(this byte value)
  109. {
  110. return System.Convert.ToString(value, 2).PadLeft(8, '0').AsSpan();
  111. }
  112. }
  113. }