Link to home
Start Free TrialLog in
Avatar of vikasbapat
vikasbapat

asked on

How to Encode URL in Base64 in C# windows application?

Hi,

I want to encode an URL in to Base64 in c# windows application.
Please help me.
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Get the URL string as byte[] and then do Convert.ToBase64String(byte[]);
Avatar of vikasbapat
vikasbapat

ASKER

This ought to work:

using System.Text;

byte[] urlBytes = Encoding.ASCII.GetBytes(url);
string urlB64 = Convert.ToBase64String(urlBytes);

If you want the URL treated as, for example, Unicode, you would change the encoding like so:

byte[] urlBytes = Encoding.Unicode.GetBytes(url);
ASKER CERTIFIED SOLUTION
Avatar of muzzy2003
muzzy2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is working fine.

Thanks for quick responce.
Thanks, I tell you how to encode to base64 and you award points for converting a string to bytes.
You could have at least split the points there.
Yes, agreed. And a B?
(BTW ozymandias, hadn't intended to steal this one, just saw it in passing. Assumed you'd already basically won the points ...)
Don't worry about it, these things happen.
I nearly came back and added the bit about converting a string to bytes but thought that was kinda overkill.