Check Base64String.

  • Thread starter Thread starter Abhi
  • Start date Start date
Abhi said:
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.
 
Jon Skeet [C# MVP] skrev:
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.
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.
 
Bjørn Brox said:
Jon Skeet [C# MVP] skrev:
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.

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.
 
Abhi said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top