ByteHelper.cs 641 B

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