views:

1038

answers:

4

Hello, is there any C# method that works similar to Convert.ToBase64String but doesn't generate anything except alphanumeric output?

Thanks!

+2  A: 

you can replace + or slash with some predefined string if possible.

TheVillageIdiot
I thought about doing that, but if I happen to hit that exact string during regular base64 encoding then hell breaks loose. Plus it would be really just a hack. :(
Alex
@Alex it is not a hack. It is used in communications. I really not remember the exact scheme (that's why such short answer) but it is possible to handle the occurrence of pattern in base64 string gracefully. I've read about it in AST's "Computer Network" and Stalling's "Data and Computer Communications" during my masters.
TheVillageIdiot
Got it. Didn't know about "-" and "_" by the way - that works I guess. Thanks so much though! :)
Alex
+4  A: 

You're probably looking at using something like Base32 encoding then. There is a Base32 encoder/decoder for C# here by Michael Giagnocavo. It uses a combination of capitalized letters and numbers.

There's also a related discussion on StackOverflow here.

EDIT: And if by any chance this is for URL-safe related Base64 encoding, just do Base64 and replace "+" with "-" and "/" with "_". But I'm guessing, you may not want it for that.

Xiaofu
Base64 will never create "-" or "_" by itself?
Alex
The method provided by the .NET libraries doesn't, no.
Xiaofu
There's a brief description on URL-safe Base64 here: http://en.wikipedia.org/wiki/Base64#URL_applications
Xiaofu
+1  A: 

A common variant of base-64 (for use on query-string) is to use '-' and '_' in place of '+' and '/'. Perhaps a bit of Replace(...) at each end would do the job?

Marc Gravell
I thought Base64 would encode by itself to "-" and "_" as well. I guess it doesn't??
Alex
No; the "classic" base-64 set is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Marc Gravell
A: 

You can use BitConverter.ToString() which will give you a hex string. However the resulting strings will be longer than Base64 encoding.

sharptooth