ByteHelper.cs 864 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Numerics;
  6. using System.Text;
  7. namespace Helper
  8. {
  9. public static class ByteHelper
  10. {
  11. public static string ToHex(this byte b)
  12. {
  13. return b.ToString("X2");
  14. }
  15. public static string ToHex(this IEnumerable<byte> bytes)
  16. {
  17. var stringBuilder = new StringBuilder();
  18. foreach (byte b in bytes)
  19. {
  20. stringBuilder.Append(b.ToString("X2"));
  21. }
  22. return stringBuilder.ToString();
  23. }
  24. public static string ToStr(this IEnumerable<byte> bytes)
  25. {
  26. var stringBuilder = new StringBuilder();
  27. foreach (byte b in bytes)
  28. {
  29. stringBuilder.Append(b.ToString(CultureInfo.InvariantCulture));
  30. }
  31. return stringBuilder.ToString();
  32. }
  33. public static byte[] Reverse(this byte[] bytes)
  34. {
  35. Array.Reverse(bytes);
  36. return bytes;
  37. }
  38. }
  39. }