How to check if provided string is Base64String.
> How to check if provided string is Base64String.
The easiest thing would be to call Convert.FromBase64String and catch
the FormatException. It's not terribly nice, and will be inefficient if
you have *lots* of these strings to convert, but it's reasonable for a
one-off. It's likely to be more reliable than writing your own
implementation.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Bjørn Brox - 07 Feb 2008 07:56 GMT
Jon Skeet [C# MVP] skrev:
>> How to check if provided string is Base64String.
>
[quoted text clipped - 3 lines]
> one-off. It's likely to be more reliable than writing your own
> implementation.
It is easier to force it as a requirement from the supplier of the string.
It it not worth the time guessing what a input field is.

Signature
Bjørn Brox
Jon Skeet [C# MVP] - 07 Feb 2008 08:23 GMT
> Jon Skeet [C# MVP] skrev:
> >> How to check if provided string is Base64String.
[quoted text clipped - 7 lines]
> It is easier to force it as a requirement from the supplier of the string.
> It it not worth the time guessing what a input field is.
We can't possibly make that decision without knowing the application in
question.
In many cases it's much, much cheaper to check the input in one place
rather than pass it down a long chain only for it to cause a fatal
error later on.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
> How to check if provided string is Base64String.
A base64 string will:
- have a length that are a multipla of 4
- consist of chars from the set
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
- end with 0, 1 or 2 '='
But it is not particular faster to check that than to actually convert.
Arne