public static string ToString(byte[] input, Action <Base32EncodingOptions> optionsFunc = null) { var defaultOptions = new Base32EncodingOptions { OmitPadding = false }; optionsFunc?.Invoke(defaultOptions); return(ToString(input, defaultOptions)); }
public static string ToString(byte[] input, Base32EncodingOptions options) { if (input == null || input.Length == 0) { throw new ArgumentNullException("input"); } var charCount = (int)Math.Ceiling(input.Length / 5d) * 8; var returnArray = new char[charCount]; byte nextChar = 0, bitsRemaining = 5; var arrayIndex = 0; foreach (var b in input) { nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining))); returnArray[arrayIndex++] = ValueToChar(nextChar); if (bitsRemaining < 4) { nextChar = (byte)((b >> (3 - bitsRemaining)) & 31); returnArray[arrayIndex++] = ValueToChar(nextChar); bitsRemaining += 5; } bitsRemaining -= 3; nextChar = (byte)((b << bitsRemaining) & 31); } //if we didn't end with a full char if (arrayIndex != charCount) { returnArray[arrayIndex++] = ValueToChar(nextChar); if (!options.OmitPadding) { while (arrayIndex != charCount) { returnArray[arrayIndex++] = '='; //padding } } } return(new string(returnArray.Slice(0, arrayIndex))); }