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.

100 lines
3.3 KiB

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. namespace JT809.Protocol.ProtocolPacket
  5. {
  6. public static class BinaryExtensions
  7. {
  8. public static void WriteLittle(this BinaryWriter writer, ulong value)
  9. {
  10. writer.Write((byte)(value >> 56));
  11. writer.Write((byte)(value >> 48));
  12. writer.Write((byte)(value >> 40));
  13. writer.Write((byte)(value >> 32));
  14. writer.Write((byte)(value >> 24));
  15. writer.Write((byte)(value >> 16));
  16. writer.Write((byte)(value >> 8));
  17. writer.Write((byte)(value));
  18. }
  19. public static void WriteLittle(this BinaryWriter writer, uint value)
  20. {
  21. writer.Write((byte)(value >> 24));
  22. writer.Write((byte)(value >> 16));
  23. writer.Write((byte)(value >> 8));
  24. writer.Write((byte)(value));
  25. }
  26. public static void WriteLittle(this BinaryWriter writer, int value)
  27. {
  28. writer.Write((byte)(value >> 24));
  29. writer.Write((byte)(value >> 16));
  30. writer.Write((byte)(value >> 8));
  31. writer.Write((byte)(value));
  32. }
  33. public static void WriteLittle(this BinaryWriter writer, short value)
  34. {
  35. writer.Write((byte)(value >> 8));
  36. writer.Write((byte)(value));
  37. }
  38. public static void WriteLittle(this BinaryWriter writer, ushort value)
  39. {
  40. writer.Write((byte)(value >> 8));
  41. writer.Write((byte)(value));
  42. }
  43. public static void WriteLittle(this BinaryWriter writer, byte value)
  44. {
  45. writer.Write(value);
  46. }
  47. public static void WriteLittle(this BinaryWriter writer, byte[] value)
  48. {
  49. writer.Write(value);
  50. }
  51. public static ulong ReadUInt64Little(this BinaryReader read)
  52. {
  53. var buffer = read.ReadBytes(8);
  54. return (ulong)(buffer[7] | buffer[6] << 8 | buffer[5] << 16 | buffer[4] << 24| buffer[3] << 32 | buffer[2] << 40 | buffer[1] << 48 | buffer[0] << 56);
  55. }
  56. public static uint ReadUInt32Little(this BinaryReader read)
  57. {
  58. var buffer = read.ReadBytes(4);
  59. return (uint)(buffer[3] | buffer[2] << 8 | buffer[1] << 16 | buffer[0] << 24);
  60. }
  61. public static ushort ReadUInt16Little(this BinaryReader read)
  62. {
  63. var buffer = read.ReadBytes(2);
  64. return (ushort)(buffer[1] | buffer[0] << 8);
  65. }
  66. /// <summary>
  67. /// 字节数组转16进制字符串
  68. /// </summary>
  69. /// <param name="bytes"></param>
  70. /// <param name="separator">默认 " "</param>
  71. /// <returns></returns>
  72. public static string ToHexString(this byte[] bytes,string separator=" ")
  73. {
  74. return string.Join(separator, bytes.Select(s => s.ToString("X2")));
  75. }
  76. /// <summary>
  77. /// 16进制字符串转16进制数组
  78. /// </summary>
  79. /// <param name="hexString"></param>
  80. /// <param name="separator"></param>
  81. /// <returns></returns>
  82. public static byte[] ToHexBytes(this string hexString, string separator = " ")
  83. {
  84. return hexString.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToByte(s, 16)).ToArray();
  85. }
  86. }
  87. }