Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

130 lignes
4.2 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. internal 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. /// <summary>
  70. /// Copyright (c) Microsoft. All rights reserved.
  71. /// Licensed under the MIT license. See LICENSE file in the project root for full license information.
  72. /// <see cref="DotNetty.Buffers.ByteBufferUtil"/>
  73. /// </summary>
  74. }
  75. public static class HexUtil
  76. {
  77. static readonly char[] HexdumpTable = new char[256 * 4];
  78. static HexUtil()
  79. {
  80. char[] digits = "0123456789ABCDEF".ToCharArray();
  81. for (int i = 0; i < 256; i++)
  82. {
  83. HexdumpTable[i << 1] = digits[(int)((uint)i >> 4 & 0x0F)];
  84. HexdumpTable[(i << 1) + 1] = digits[i & 0x0F];
  85. }
  86. }
  87. public static string DoHexDump(ReadOnlySpan<byte> buffer, int fromIndex, int length)
  88. {
  89. if (length == 0)
  90. {
  91. return "";
  92. }
  93. int endIndex = fromIndex + length;
  94. var buf = new char[length << 1];
  95. int srcIdx = fromIndex;
  96. int dstIdx = 0;
  97. for (; srcIdx < endIndex; srcIdx++, dstIdx += 2)
  98. {
  99. Array.Copy(HexdumpTable, buffer[srcIdx] << 1, buf, dstIdx, 2);
  100. }
  101. return new string(buf);
  102. }
  103. public static string DoHexDump(byte[] array, int fromIndex, int length)
  104. {
  105. if (length == 0)
  106. {
  107. return "";
  108. }
  109. int endIndex = fromIndex + length;
  110. var buf = new char[length << 1];
  111. int srcIdx = fromIndex;
  112. int dstIdx = 0;
  113. for (; srcIdx < endIndex; srcIdx++, dstIdx += 2)
  114. {
  115. Array.Copy(HexdumpTable, (array[srcIdx] & 0xFF) << 1, buf, dstIdx, 2);
  116. }
  117. return new string(buf);
  118. }
  119. }
  120. }