/// <summary> /// Converts a base-32 encoded string to a byte array. /// </summary> /// <param name="base32">A base-32 encoded string.</param> /// <returns>The data represented by the base-32 string.</returns> /// <exception cref="ArgumentException">The argument is not a valid base32-encoded string.</exception> public static byte[] ToBinary(string base32) { base32 = base32.ToUpper(); if (base32.Any(c => !Alphabet.Contains(c) && c != Padding)) { throw new ArgumentException("String contains invalid characters."); } var fullSegments = base32.Length / 8; var finalSegmentLength = base32.Length % 8; var segments = fullSegments + (finalSegmentLength == 0 ? 0 : 1); IEnumerable <byte> result = new byte[0]; for (int i = 0; i < segments; i++) { var segment = base32.Skip(i * 8).Take(8).ToArray(); var slice = Base32.ConvertSegmentToBinary(segment); result = result.Concat(slice); } return(result.ToArray()); }