Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Check Base64String.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abhi - 07 Feb 2008 06:37 GMT
How to check if provided string is Base64String.
Jon Skeet [C# MVP] - 07 Feb 2008 07:38 GMT
> 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

Arne Vajhøj - 10 Feb 2008 22:11 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.