ByteUtil.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace HKCarPlateRecognize_App
  6. {
  7. public static class ByteUtil
  8. {
  9. ///右补齐
  10. public static byte[] PadRight(this IEnumerable<byte> SourceBytes, int TotalLength, byte PaddingByte)
  11. {
  12. if (TotalLength <= SourceBytes.Count()) return SourceBytes.ToArray();
  13. var TargetBytes = new byte[TotalLength];
  14. for (int i = 0; i < SourceBytes.Count(); i++)
  15. TargetBytes[i] = SourceBytes.ElementAt(i);
  16. for (int i = SourceBytes.Count() ; i < TotalLength; i++)
  17. TargetBytes[i] = PaddingByte;
  18. return TargetBytes;
  19. }
  20. ///左补齐
  21. public static byte[] PadLeft(this IEnumerable<byte> SourceBytes, int TotalLength, byte PaddingByte)
  22. {
  23. if (TotalLength <= SourceBytes.Count()) return SourceBytes.ToArray();
  24. var TargetBytes = new byte[TotalLength];
  25. for (int i = 0; i < TotalLength - SourceBytes.Count(); i++)
  26. TargetBytes[i] = PaddingByte;
  27. for (int i = 0; i < SourceBytes.Count(); i++)
  28. TargetBytes[TotalLength - SourceBytes.Count() + i] = SourceBytes.ElementAt(i);
  29. return TargetBytes;
  30. }
  31. ///字节数组转为十六进制字符串
  32. public static string BytesToHexString(this byte[] Bytes)
  33. {
  34. var HexString = new StringBuilder();
  35. for (int i = 0; i < Bytes.Length; i++)
  36. {
  37. HexString.Append(Bytes[i].ToString("X").PadLeft(2, '0'));
  38. }
  39. return HexString.ToString();
  40. }
  41. ///字节数组转为十六进制字符串
  42. public static string BytesToHexString(this byte[] Bytes, int Offset, int Length)
  43. {
  44. var HexString = "";
  45. for (int i = Offset; i < (Offset + Length); i++)
  46. {
  47. HexString += Bytes[i].ToString("X").PadLeft(2, '0');
  48. }
  49. return HexString;
  50. }
  51. ///十六进制字符串转为字节数组
  52. public static byte[] HexStringToBytes(this string HexStr)
  53. {
  54. var NewHexStr = "";
  55. foreach (char Hex in HexStr)
  56. if (Uri.IsHexDigit(Hex))
  57. NewHexStr += Hex;
  58. if (NewHexStr.Length % 2 != 0) NewHexStr = NewHexStr.PadLeft(NewHexStr.Length + 1, '0');
  59. byte[] Buffer = new byte[NewHexStr.Length / 2];
  60. for (int i = 0; i < NewHexStr.Length; i += 2)
  61. Buffer[i / 2] = (byte)Convert.ToByte(NewHexStr.Substring(i, 2), 16);
  62. return Buffer;
  63. }
  64. ///获取字节数组的一部分
  65. public static byte[] GetPartBytes(this IEnumerable<byte> Bytes, int OffSet, int Length)
  66. {
  67. var PartBytes = new byte[Length];
  68. Array.Copy(Bytes.ToArray(), OffSet, PartBytes, 0, Length);
  69. return PartBytes;
  70. }
  71. ///获取字节数组的一部分
  72. public static byte[] GetPartBytes(this IEnumerable<byte> Bytes, int OffSet)
  73. {
  74. var PartBytes = new byte[Bytes.Count() - OffSet];
  75. Array.Copy(Bytes.ToArray(), OffSet, PartBytes, 0, PartBytes.Length);
  76. return PartBytes;
  77. }
  78. ///比较字节数组内容是否相等
  79. public static bool EnumerableEquals(this IEnumerable<byte> Bytes, IEnumerable<byte> OtherBytes)
  80. {
  81. if (Bytes == null || OtherBytes == null) return false;
  82. if (Bytes.Count() != OtherBytes.Count()) return false;
  83. for (int i = 0; i < Bytes.Count(); i++)
  84. {
  85. if (Bytes.ElementAt(i) != OtherBytes.ElementAt(i)) return false;
  86. }
  87. return true;
  88. }
  89. }
  90. }