Hex.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. namespace MediaBrowser.Common
  4. {
  5. /// <summary>
  6. /// Encoding and decoding hex strings.
  7. /// </summary>
  8. public static class Hex
  9. {
  10. internal const string HexCharsLower = "0123456789abcdef";
  11. internal const string HexCharsUpper = "0123456789ABCDEF";
  12. internal const int LastHexSymbol = 0x66; // 102: f
  13. /// <summary>
  14. /// Gets a map from an ASCII char to its hex value shifted,
  15. /// e.g. <c>b</c> -> 11. 0xFF means it's not a hex symbol.
  16. /// </summary>
  17. internal static ReadOnlySpan<byte> HexLookup => new byte[]
  18. {
  19. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  20. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  21. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  22. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  23. 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  24. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  25. 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  26. };
  27. /// <summary>
  28. /// Encodes each element of the specified bytes as its hexadecimal string representation.
  29. /// </summary>
  30. /// <param name="bytes">An array of bytes.</param>
  31. /// <param name="lowercase"><c>true</c> to use lowercase hexadecimal characters; otherwise <c>false</c>.</param>
  32. /// <returns><c>bytes</c> as a hex string.</returns>
  33. public static string Encode(ReadOnlySpan<byte> bytes, bool lowercase = true)
  34. {
  35. var hexChars = lowercase ? HexCharsLower : HexCharsUpper;
  36. // TODO: use string.Create when it's supports spans
  37. // Ref: https://github.com/dotnet/corefx/issues/29120
  38. char[] s = new char[bytes.Length * 2];
  39. int j = 0;
  40. for (int i = 0; i < bytes.Length; i++)
  41. {
  42. s[j++] = hexChars[bytes[i] >> 4];
  43. s[j++] = hexChars[bytes[i] & 0x0f];
  44. }
  45. return new string(s);
  46. }
  47. /// <summary>
  48. /// Decodes a hex string into bytes.
  49. /// </summary>
  50. /// <param name="str">The <see cref="string" />.</param>
  51. /// <returns>The decoded bytes.</returns>
  52. public static byte[] Decode(ReadOnlySpan<char> str)
  53. {
  54. if (str.Length == 0)
  55. {
  56. return Array.Empty<byte>();
  57. }
  58. var unHex = HexLookup;
  59. int byteLen = str.Length / 2;
  60. byte[] bytes = new byte[byteLen];
  61. int i = 0;
  62. for (int j = 0; j < byteLen; j++)
  63. {
  64. byte a;
  65. byte b;
  66. if (str[i] > LastHexSymbol
  67. || (a = unHex[str[i++]]) == 0xFF
  68. || str[i] > LastHexSymbol
  69. || (b = unHex[str[i++]]) == 0xFF)
  70. {
  71. ThrowArgumentException(nameof(str));
  72. break; // Unreachable
  73. }
  74. bytes[j] = (byte)((a * 16) | b);
  75. }
  76. return bytes;
  77. }
  78. [DoesNotReturn]
  79. private static void ThrowArgumentException(string paramName)
  80. => throw new ArgumentException("Character is not a hex symbol.", paramName);
  81. }
  82. }