ByteHelper.cs 1015 B

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