Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

172 linhas
5.7 KiB

  1. using System;
  2. namespace JT808.Protocol.Extensions
  3. {
  4. /// <summary>
  5. ///
  6. /// ref:"www.codeproject.com/tips/447938/high-performance-csharp-byte-array-to-hex-string-t"
  7. /// </summary>
  8. public static partial class JT808BinaryExtensions
  9. {
  10. public static string ToHexString(this byte[] source)
  11. {
  12. return HexUtil.DoHexDump(source, 0, source.Length).ToUpper();
  13. }
  14. public static int WriteHexStringLittle(byte[] bytes, int offset, string data, int len)
  15. {
  16. if (data == null) data = "";
  17. data = data.Replace(" ", "");
  18. int startIndex = 0;
  19. if (data.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
  20. {
  21. startIndex = 2;
  22. }
  23. int length = len;
  24. if (length == -1)
  25. {
  26. length = (data.Length - startIndex) / 2;
  27. }
  28. int noOfZero = length * 2 + startIndex - data.Length;
  29. if (noOfZero > 0)
  30. {
  31. data = data.Insert(startIndex, new string('0', noOfZero));
  32. }
  33. int byteIndex = 0;
  34. while (startIndex < data.Length && byteIndex < length)
  35. {
  36. bytes[offset + byteIndex] = Convert.ToByte(data.Substring(startIndex, 2), 16);
  37. startIndex += 2;
  38. byteIndex++;
  39. }
  40. return length;
  41. }
  42. /// <summary>
  43. /// 16进制字符串转16进制数组
  44. /// </summary>
  45. /// <param name="hexString"></param>
  46. /// <param name="separator"></param>
  47. /// <returns></returns>
  48. public static byte[] ToHexBytes(this string hexString)
  49. {
  50. hexString = hexString.Replace(" ", "");
  51. byte[] buf = new byte[hexString.Length / 2];
  52. ReadOnlySpan<char> readOnlySpan = hexString.AsSpan();
  53. for (int i = 0; i < hexString.Length; i++)
  54. {
  55. if (i % 2 == 0)
  56. {
  57. buf[i / 2] = Convert.ToByte(readOnlySpan.Slice(i, 2).ToString(), 16);
  58. }
  59. }
  60. return buf;
  61. }
  62. public static string ReadHexStringLittle(ReadOnlySpan<byte> read, ref int offset, int len)
  63. {
  64. //ReadOnlySpan<byte> source = read.Slice(offset, len);
  65. string hex = HexUtil.DoHexDump(read, offset, len);
  66. offset += len;
  67. return hex;
  68. }
  69. public static string ReadNumber(this byte value, string format = "X2")
  70. {
  71. return value.ToString(format);
  72. }
  73. public static string ReadNumber(this int value, string format = "X8")
  74. {
  75. return value.ToString(format);
  76. }
  77. public static string ReadNumber(this uint value, string format = "X8")
  78. {
  79. return value.ToString(format);
  80. }
  81. public static string ReadNumber(this long value, string format = "X16")
  82. {
  83. return value.ToString(format);
  84. }
  85. public static string ReadNumber(this ulong value, string format="X16")
  86. {
  87. return value.ToString(format);
  88. }
  89. public static string ReadNumber(this short value, string format = "X4")
  90. {
  91. return value.ToString(format);
  92. }
  93. public static string ReadNumber(this ushort value, string format = "X4")
  94. {
  95. return value.ToString(format);
  96. }
  97. public static ReadOnlySpan<char> ReadBinary(this ushort value)
  98. {
  99. return System.Convert.ToString(value, 2).PadLeft(16, '0').AsSpan();
  100. }
  101. public static ReadOnlySpan<char> ReadBinary(this short value)
  102. {
  103. return System.Convert.ToString(value, 2).PadLeft(16, '0').AsSpan();
  104. }
  105. public static ReadOnlySpan<char> ReadBinary(this uint value)
  106. {
  107. return System.Convert.ToString(value, 2).PadLeft(32, '0').AsSpan();
  108. }
  109. public static ReadOnlySpan<char> ReadBinary(this int value)
  110. {
  111. return System.Convert.ToString(value, 2).PadLeft(32, '0').AsSpan();
  112. }
  113. public static ReadOnlySpan<char> ReadBinary(this byte value)
  114. {
  115. return System.Convert.ToString(value, 2).PadLeft(8, '0').AsSpan();
  116. }
  117. }
  118. public static class HexUtil
  119. {
  120. static readonly char[] HexdumpTable = new char[256 * 4];
  121. static HexUtil()
  122. {
  123. char[] digits = "0123456789ABCDEF".ToCharArray();
  124. for (int i = 0; i < 256; i++)
  125. {
  126. HexdumpTable[i << 1] = digits[(int)((uint)i >> 4 & 0x0F)];
  127. HexdumpTable[(i << 1) + 1] = digits[i & 0x0F];
  128. }
  129. }
  130. public static string DoHexDump(ReadOnlySpan<byte> buffer, int fromIndex, int length)
  131. {
  132. if (length == 0)
  133. {
  134. return "";
  135. }
  136. int endIndex = fromIndex + length;
  137. var buf = new char[length << 1];
  138. int srcIdx = fromIndex;
  139. int dstIdx = 0;
  140. for (; srcIdx < endIndex; srcIdx++, dstIdx += 2)
  141. {
  142. Array.Copy(HexdumpTable, buffer[srcIdx] << 1, buf, dstIdx, 2);
  143. }
  144. return new string(buf);
  145. }
  146. public static string DoHexDump(byte[] array, int fromIndex, int length)
  147. {
  148. if (length == 0)
  149. {
  150. return "";
  151. }
  152. int endIndex = fromIndex + length;
  153. var buf = new char[length << 1];
  154. int srcIdx = fromIndex;
  155. int dstIdx = 0;
  156. for (; srcIdx < endIndex; srcIdx++, dstIdx += 2)
  157. {
  158. Array.Copy(HexdumpTable, (array[srcIdx] & 0xFF) << 1, buf, dstIdx, 2);
  159. }
  160. return new string(buf);
  161. }
  162. }
  163. }