src/NetBase32/ZBase32.cs
/// <summary>
/// Converts the value of an array of 8-bit unsigned integers to its equivalent string
/// representation that is encoded in z-base-32.
/// </summary>
/// <param name="data">
/// An array of 8-bit unsigned integers.
/// </param>
/// <param name="formatOptions">
/// <see cref="FormatOptions.IncludeDashes"/> to include a "-" character after every 8
/// characters or <see cref="FormatOptions.None"/> to exclude dashes.
/// </param>
/// <returns>
/// The string representation in z-base-32 of <paramref name="data"/>.
/// </returns>
Converts the value of an array of 8-bit unsigned integers to its equivalent string
representation that is encoded in z-base-32.
Converts the value of an array of 8 - bit unsigned integers to its equivalent string representation that is encoded in z - base - 32 .
public static string Encode(byte[] data, FormatOptions formatOptions = FormatOptions.IncludeDashes)
        {
            if (data.Length == 0)
            {
                return string.Empty;
            }
            var length = (int)Math.Ceiling(data.Length * 8 / 5.0);
            var includeFormatting = formatOptions == FormatOptions.IncludeDashes;
            if (includeFormatting)
            {
                var dashes = length / 8;
                if (length % 8 == 0)
                {
                    dashes--;
                }
                length += dashes;
            }
            var result = new char[length];
            var j = 0;
            var increment = includeFormatting ? 9 : 8;
            for (int i = 0; i < data.Length; i += 5)
            {
                result[j] = alphabet[(data[i]) >> 3];
                if (i + 1 >= data.Length)
                {
                    result[j + 1] = alphabet[(data[i] & 0x07) << 2];
                    break;
                }
                result[j + 1] = alphabet[((data[i] & 0x07) << 2) | ((data[i + 1] & 0xC0) >> 6)];
                result[j + 2] = alphabet[(data[i + 1] & 0x3E) >> 1];
                if (i + 2 >= data.Length)
                {
                    result[j + 3] = alphabet[(data[i + 1] & 0x01) << 4];
                    break;
                }
                result[j + 3] = alphabet[((data[i + 1] & 0x01) << 4) | (data[i + 2] >> 4)];
                if (i + 3 >= data.Length)
                {
                    result[j + 4] = alphabet[(data[i + 2] & 0x0F) << 1];
                    break;
                }
                result[j + 4] = alphabet[((data[i + 2] & 0x0F) << 1) | ((data[i + 3] & 0x80) >> 7)];
                result[j + 5] = alphabet[(data[i + 3] & 0x7C) >> 2];
                if (i + 4 >= data.Length)
                {
                    result[j + 6] = alphabet[(data[i + 3] & 0x03) << 3];
                    break;
                }
                result[j + 6] = alphabet[((data[i + 3] & 0x03) << 3) | (data[i + 4] >> 5)];
                result[j + 7] = alphabet[data[i + 4] & 0x1F];
                if (i + 5 < data.Length && includeFormatting)
                {
                    result[j + 8] = '-';
                }
                j += increment;
            }
            return new string(result);
        }
public static string Encode ( byte [ ] data , FormatOptions formatOptions = FormatOptions . IncludeDashes ) { if ( data . Length == 0 ) { return string . Empty ; } var length = ( int ) Math . Ceiling ( data . Length * 8 / 5.0 ) ; var includeFormatting = formatOptions == FormatOptions . IncludeDashes ; if ( includeFormatting ) { var dashes = length / 8 ; if ( length % 8 == 0 ) { dashes -- ; } length += dashes ; } var result = new char [ length ] ; var j = 0 ; var increment = includeFormatting ? 9 : 8 ; for ( int i = 0 ; i < data . Length ; i += 5 ) { result [ j ] = alphabet [ ( data [ i ] ) >> 3 ] ; if ( i + 1 >= data . Length ) { result [ j + 1 ] = alphabet [ ( data [ i ] & 0x07 ) << 2 ] ; break ; } result [ j + 1 ] = alphabet [ ( ( data [ i ] & 0x07 ) << 2 ) | ( ( data [ i + 1 ] & 0xC0 ) >> 6 ) ] ; result [ j + 2 ] = alphabet [ ( data [ i + 1 ] & 0x3E ) >> 1 ] ; if ( i + 2 >= data . Length ) { result [ j + 3 ] = alphabet [ ( data [ i + 1 ] & 0x01 ) << 4 ] ; break ; } result [ j + 3 ] = alphabet [ ( ( data [ i + 1 ] & 0x01 ) << 4 ) | ( data [ i + 2 ] >> 4 ) ] ; if ( i + 3 >= data . Length ) { result [ j + 4 ] = alphabet [ ( data [ i + 2 ] & 0x0F ) << 1 ] ; break ; } result [ j + 4 ] = alphabet [ ( ( data [ i + 2 ] & 0x0F ) << 1 ) | ( ( data [ i + 3 ] & 0x80 ) >> 7 ) ] ; result [ j + 5 ] = alphabet [ ( data [ i + 3 ] & 0x7C ) >> 2 ] ; if ( i + 4 >= data . Length ) { result [ j + 6 ] = alphabet [ ( data [ i + 3 ] & 0x03 ) << 3 ] ; break ; } result [ j + 6 ] = alphabet [ ( ( data [ i + 3 ] & 0x03 ) << 3 ) | ( data [ i + 4 ] >> 5 ) ] ; result [ j + 7 ] = alphabet [ data [ i + 4 ] & 0x1F ] ; if ( i + 5 < data . Length && includeFormatting ) { result [ j + 8 ] = ' ' ; } j += increment ; } return new string ( result ) ; }
Converts the value of an array of 8-bit unsigned integers to its equivalent string
representation that is encoded in z-base-32.
Converts the value of an array of 8 - bit unsigned integers to its equivalent string representation that is encoded in z - base - 32 .
//Every 5 bytes will output 8 characters. //Append a '-' at the end of each 8 character block for readability.
The string representation in z-base-32 of .
The string representation in z - base - 32 of .
data
byte[]
An array of 8-bit unsigned integers.
An array of 8 - bit unsigned integers .
formatOptions
FormatOptions
to include a "-" character after every 8
characters or  to exclude dashes.
to include a " - " character after every 8 characters or to exclude dashes .