版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

101 行
3.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ProductionControl.Utils
  7. {
  8. public static class HexUtil
  9. {
  10. public static byte[] joinByteList(List<byte[]> buffList)
  11. {
  12. int len = 0;
  13. foreach (byte[] b in buffList)
  14. len += b.Length;
  15. byte[] result = new byte[len];
  16. len = 0;
  17. foreach (byte[] b in buffList)
  18. {
  19. b.CopyTo(result, len);
  20. len += b.Length;
  21. }
  22. return result;
  23. }
  24. public static byte[] subBytes(byte[] buff, int start, int len = 0)
  25. {
  26. if (len < 1)
  27. return buff.Skip(start).ToArray();
  28. else
  29. return buff.Skip(start).Take(len).ToArray();
  30. }
  31. public static string toHexString(this byte[] bytes)
  32. {
  33. string byteStr = string.Empty;
  34. if (bytes != null || bytes.Length > 0)
  35. {
  36. foreach (var item in bytes)
  37. {
  38. byteStr += string.Format("{0:X2}", item);
  39. }
  40. }
  41. return byteStr;
  42. }
  43. public static bool Equals(byte[] buff1, byte[] buff2)
  44. {
  45. if (buff1 == null || buff1 == null || buff1.Length != buff2.Length)
  46. return false;
  47. for (int i = 0; i < buff1.Length; i++)
  48. if (buff1[i] != buff2[i]) return false;
  49. return true;
  50. }
  51. public static IEnumerable<long> IndexOf(byte[] source, int start, byte[] pattern)
  52. {
  53. if (source == null)
  54. {
  55. throw new ArgumentNullException(nameof(source));
  56. }
  57. if (pattern == null)
  58. {
  59. throw new ArgumentNullException(nameof(pattern));
  60. }
  61. long valueLength = source.LongLength;
  62. long patternLength = pattern.LongLength;
  63. if ((valueLength == 0) || (patternLength == 0) || (patternLength > valueLength))
  64. {
  65. yield break;
  66. }
  67. var badCharacters = new long[256];
  68. for (var i = 0; i < 256; i++)
  69. {
  70. badCharacters[i] = patternLength;
  71. }
  72. var lastPatternByte = patternLength - 1;
  73. for (long i = 0; i < lastPatternByte; i++)
  74. {
  75. badCharacters[pattern[i]] = lastPatternByte - i;
  76. }
  77. long index = start;
  78. while (index <= valueLength - patternLength)
  79. {
  80. for (var i = lastPatternByte; source[index + i] == pattern[i]; i--)
  81. {
  82. if (i == 0)
  83. {
  84. yield return index;
  85. break;
  86. }
  87. }
  88. index += badCharacters[source[index + lastPatternByte]];
  89. }
  90. }
  91. }
  92. }