src/NetBase32/ZBase32.cs
/// <summary>
/// Converts the specified string, which represents binary data encoded in z-base-32, to an
/// equivalent 8-bit unsigned integer array.
/// </summary>
/// <param name="data">
/// The string to decode.
/// </param>
/// <returns>
/// An array of 8-bit unsigned integers that is equivalent to <paramref name="data"/>.
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="data"/> is not a valid z-base-32 encoded string.
/// </exception>
Converts the specified string, which represents binary data encoded in z-base-32, to an
equivalent 8-bit unsigned integer array.
Converts the specified string which represents binary data encoded in z - base - 32 to an equivalent 8 - bit unsigned integer array .
public static byte[] Decode(string data)
        {
            if (data.Length == 0)
            {
                return Array.Empty<byte>();
            }
            var inputChars = data.ToCharArray();
            Validate(inputChars);
            var inputLength = inputChars.Length;
            int outputLength = inputLength * 5 / 8;
            int increment = 8;
            if (inputLength >= 9 && inputChars[8] == '-')
            {
                outputLength = (inputLength - (inputLength / 9)) * 5 / 8;
                increment = 9;
            }
            var result = new byte[outputLength];
            var j = 0;
            for (int i = 0; i < outputLength; i += 5)
            {
                result[i] = (byte)(decoder[inputChars[j]] << 3);
                if (j + 1 >= inputLength)
                    break;
                result[i] |= (byte)(decoder[inputChars[j + 1]] >> 2);
                if (i + 1 >= outputLength)
                    break;
                result[i + 1] = (byte)(decoder[inputChars[j + 1]] << 6);
                if (j + 2 >= inputLength)
                    break;
                result[i + 1] |= (byte)(decoder[inputChars[j + 2]] << 1);
                if (j + 3 >= inputLength)
                    break;
                result[i + 1] |= (byte)(decoder[inputChars[j + 3]] >> 4);
                if (i + 2 >= outputLength)
                    break;
                result[i + 2] = (byte)(decoder[inputChars[j + 3]] << 4);
                if (j + 4 >= inputLength)
                    break;
                result[i + 2] |= (byte)(decoder[inputChars[j + 4]] >> 1);
                if (i + 3 >= outputLength)
                    break;
                result[i + 3] = (byte)(decoder[inputChars[j + 4]] << 7);
                if (j + 5 >= inputLength)
                    break;
                result[i + 3] |= (byte)(decoder[inputChars[j + 5]] << 2);
                if (j + 6 >= inputLength)
                    break;
                result[i + 3] |= (byte)(decoder[inputChars[j + 6]] >> 3);
                if (i + 4 >= outputLength)
                    break;
                result[i + 4] = (byte)(decoder[inputChars[j + 6]] << 5);
                if (j + 7 >= inputLength)
                    break;
                result[i + 4] |= decoder[inputChars[j + 7]];
                j += increment;
            }
            return result;
        }
public static byte [ ] Decode ( string data ) { if ( data . Length == 0 ) { return Array . Empty < byte > ( ) ; } var inputChars = data . ToCharArray ( ) ; Validate ( inputChars ) ; var inputLength = inputChars . Length ; int outputLength = inputLength * 5 / 8 ; int increment = 8 ; if ( inputLength >= 9 && inputChars [ 8 ] == ' ' ) { outputLength = ( inputLength - ( inputLength / 9 ) ) * 5 / 8 ; increment = 9 ; } var result = new byte [ outputLength ] ; var j = 0 ; for ( int i = 0 ; i < outputLength ; i += 5 ) { result [ i ] = ( byte ) ( decoder [ inputChars [ j ] ] << 3 ) ; if ( j + 1 >= inputLength ) break ; result [ i ] |= ( byte ) ( decoder [ inputChars [ j + 1 ] ] >> 2 ) ; if ( i + 1 >= outputLength ) break ; result [ i + 1 ] = ( byte ) ( decoder [ inputChars [ j + 1 ] ] << 6 ) ; if ( j + 2 >= inputLength ) break ; result [ i + 1 ] |= ( byte ) ( decoder [ inputChars [ j + 2 ] ] << 1 ) ; if ( j + 3 >= inputLength ) break ; result [ i + 1 ] |= ( byte ) ( decoder [ inputChars [ j + 3 ] ] >> 4 ) ; if ( i + 2 >= outputLength ) break ; result [ i + 2 ] = ( byte ) ( decoder [ inputChars [ j + 3 ] ] << 4 ) ; if ( j + 4 >= inputLength ) break ; result [ i + 2 ] |= ( byte ) ( decoder [ inputChars [ j + 4 ] ] >> 1 ) ; if ( i + 3 >= outputLength ) break ; result [ i + 3 ] = ( byte ) ( decoder [ inputChars [ j + 4 ] ] << 7 ) ; if ( j + 5 >= inputLength ) break ; result [ i + 3 ] |= ( byte ) ( decoder [ inputChars [ j + 5 ] ] << 2 ) ; if ( j + 6 >= inputLength ) break ; result [ i + 3 ] |= ( byte ) ( decoder [ inputChars [ j + 6 ] ] >> 3 ) ; if ( i + 4 >= outputLength ) break ; result [ i + 4 ] = ( byte ) ( decoder [ inputChars [ j + 6 ] ] << 5 ) ; if ( j + 7 >= inputLength ) break ; result [ i + 4 ] |= decoder [ inputChars [ j + 7 ] ] ; j += increment ; } return result ; }
Converts the specified string, which represents binary data encoded in z-base-32, to an
equivalent 8-bit unsigned integer array.
Converts the specified string which represents binary data encoded in z - base - 32 to an equivalent 8 - bit unsigned integer array .
//Indexing is faster on char[] than it is on string, so convert the input to a char array before processing. //All characters are data characters by default. //Base 32 causes a 8/5 expansion. We also have to account for the '-' character that is //appended after every 8 characters of data.  //Every 9th character is a non-data character added for readabiltiy so use 8 //characters and skip the 9th.
An array of 8-bit unsigned integers that is equivalent to .
An array of 8 - bit unsigned integers that is equivalent to .
Thrown when  is not a valid z-base-32 encoded string.
Thrown when is not a valid z - base - 32 encoded string .
data
string
The string to decode.
The string to decode .