using System; using System.Linq; namespace Swan { /// /// Provides various extension methods for byte arrays and streams. /// public static class ByteArrayExtensions { /// /// Converts an array of bytes to a base-64 encoded string. /// /// The bytes. /// A converted from an array of bytes. public static String ToBase64(this Byte[] bytes) => Convert.ToBase64String(bytes); /// /// Converts a set of hexadecimal characters (uppercase or lowercase) /// to a byte array. String length must be a multiple of 2 and /// any prefix (such as 0x) has to be avoided for this to work properly. /// /// The hexadecimal. /// /// A byte array containing the results of encoding the specified set of characters. /// /// hex. public static Byte[] ConvertHexadecimalToBytes(this String @this) { if(String.IsNullOrWhiteSpace(@this)) { throw new ArgumentNullException(nameof(@this)); } return Enumerable.Range(0, @this.Length / 2).Select(x => Convert.ToByte(@this.Substring(x * 2, 2), 16)).ToArray(); } } }