Invalid length for a Base-64 char array

Posted by VIJI Sunday, July 5, 2009

The error Invalid length for a Base-64 char array usually occurs when calling the function

Convert.FromBase64String() to convert the given string bytes array.


Possible reasons

1. The Length of the String is not a multiple of 4.

2. The String may contain ignorable characters such as White Space Characters.

Thus the invalid characters in the string are getting ignored. Thus the length of the string is not a multiple of 4.


Possible Solutions

1. Make Sure that the Length of a string is a multiple of 4.

2. Use the Modulus Operator (%) to check whether the length of the String % 4 is equal to Zero.

Example:


If (Str.Trim.Length % 4 == 0 ) Then

bitmapdate = Convert.FromBase64String(str_lbl)

End If



3. Replace the Ignorable characters with valid characters.


4. For example Replace White space characters in the character array with "+" Sign.




str = str.Replace(" ","+")

If (Str.Trim.Length % 4 == 0 ) Then

bitmapdate = Convert.FromBase64String(str_lbl)

End If

0 Responses to Invalid length for a Base-64 char array

Post a Comment