C# Method like Base64String, but only alphanumeric (no plus or slash)

11,280

Solution 1

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.

Solution 2

The answers are a bit outdated now. For the benefit of future searchers: The best way to handle this now in C# is:

byte[] b; // fill your byte array somehow
string s = System.Web.HttpServerUtility.UrlTokenEncode(b);

This returns a Base64-encoded string that is URL-safe (which is what you said you were really after in the comments to your question).

You can then decode it again using, you guessed it:

byte[] b = System.Web.HttpServerUtility.UrlTokenDecode(s);

Solution 3

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?

Solution 4

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

Solution 5

I was searching for a such like encoder and I used https://github.com/renmengye/base62-csharp/

It's an encoder/decoder in "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" character space range.

I just wrapped its methods with

System.Text.Encoding.UTF8.GetBytes(plainText); and System.Text.Encoding.UTF8.GetString(decoded);

calls to allow it to work easily with strings.

Share:
11,280
Alex
Author by

Alex

Updated on July 29, 2022

Comments

  • Alex
    Alex 4 months

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

    Thanks!

    • Smashery
      Smashery over 13 years
      So you're really after base 62 encoding?
    • Xiaofu
      Xiaofu over 13 years
      Can you tell us your specific reason for not wanting the plus or slash from Base64, just out of interest?
    • Alex
      Alex over 13 years
      I'm using the encoded string in a URL afterwards, and the random slashes are messing with my URL routing, while the + is disallowed by my IIS7 settings (the one where you can double encode with +, forgot the name).
    • pettys
      pettys over 1 year
      base 62 encoding - yes, please! (See @AFract answer below)
  • Alex
    Alex over 13 years
    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. :(
  • TheVillageIdiot
    TheVillageIdiot over 13 years
    @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.
  • Alex
    Alex over 13 years
    I thought Base64 would encode by itself to "-" and "_" as well. I guess it doesn't??
  • Xiaofu
    Xiaofu over 13 years
    The method provided by the .NET libraries doesn't, no.
  • Alex
    Alex over 13 years
    Got it. Didn't know about "-" and "_" by the way - that works I guess. Thanks so much though! :)
  • Marc Gravell
    Marc Gravell over 13 years
    No; the "classic" base-64 set is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456‌​789+/"
  • Xiaofu
    Xiaofu over 13 years
    There's a brief description on URL-safe Base64 here: en.wikipedia.org/wiki/Base64#URL_applications
  • Hele
    Hele about 6 years
    Always ends with a 0 for some reason.
  • Mason G. Zhwiti
    Mason G. Zhwiti about 6 years
    @Hele are you saying the code doesn't work? I.e. I don't understand why it matters if your encoded strings end in 0.
  • Mason G. Zhwiti
    Mason G. Zhwiti about 6 years
    @Hele What does base64 encoding have to do with generating random strings?
  • Hele
    Hele about 6 years
    Just saying it's a waste. Why is it there?
  • Mason G. Zhwiti
    Mason G. Zhwiti about 6 years
    @Hele My guess is whatever you're encoding simply has the trait that it ends with zeros when encoded in base64. If you try different strings with different lengths, you may get a different result.
  • pettys
    pettys over 1 year
    Yes! This is what I'm looking for. Not for URL's, though - just representing a string of bytes in an easy copy/pasteable way.