Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Naive bijective function in C#. http://stackoverflow.com/questions/742013
namespace Alphabet
{
public class AlphabetTest
{
public static readonly string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
public static readonly int Base = Alphabet.Length;
public static string Encode(int i)
{
if (i == 0) return Alphabet[0].ToString();
var s = string.Empty;
while (i > 0)
{
s += Alphabet[i % Base];
i = i / Base;
}
return string.Join(string.Empty, s.Reverse());
}
public static int Decode(string s)
{
var i = 0;
foreach (var c in s)
{
i = (i * Base) + Alphabet.IndexOf(c);
}
return i;
}
public static void Main(string[] args)
{
// Simple test of encode/decode operations
for (var i = 0; i < 10000; i++)
{
if (Decode(Encode(i)) != i)
{
System.Console.WriteLine("{0} is not {1}", Encode(i), i);
break;
}
}
}
}
}
@harrypatton

This comment has been minimized.

Copy link

commented May 30, 2015

Nice coding, I like the predefined char array to avoid the HashMap mentioned in Stackoverflow answer. A couple of thoughts,

  1. If change the line16 to put new char in the front, we don't need to call Reverse in the end.
  2. Using StringBuilder may gain better performance? Frankly I'm not sure, because the max encoded string would have a small length. It is probably cheaper when just copying the string over and over again by s+= newstr.
@vketsenko

This comment has been minimized.

Copy link

commented Aug 1, 2016

In Encode method instead of checking for (i==0) you could check for (i < Base) and return Alphabet[i].ToString();

@Ghanshyam-K-Dobariya

This comment has been minimized.

Copy link

commented Aug 2, 2016

Just putting my imagination..

Can Alphabet.IndexOf(c) be replaced with an associative array ??

Alphabet.IndexOf(c) will increase time as it will search everytime..
use of associative array can reduce execution time.
http://stackoverflow.com/questions/10250232/what-is-the-easiest-way-to-handle-associative-array-in-c

@aloisdg

This comment has been minimized.

Copy link

commented Sep 8, 2016

I think you are missing the "aa, ab, ac" part.

@suren2k44

This comment has been minimized.

Copy link

commented Sep 20, 2016

Hi,
Decoding function is returning value in minus,
if I am passing the URL of any text file like as
int randomnumber = CreateShortURL.Decode("http://hostname/Documents/List_2016-09-09.txt");

below is function:

public static int Decode(string s)
{
var i = 0;

            foreach (var c in s)
            {

                i = (i * Base) + Alphabet.IndexOf(c);
            }

            return i;
        }

Due to that I'm not able to receive the shortUrl value from Encode function.
Please help.

Thanks, Suren

@SlyDen

This comment has been minimized.

Copy link

commented Feb 16, 2018

@suren2k44 But the idea is that it doesn't encode free text string ... it encodes Integer - e.g. the generated ID in database table where you store that link. So the result of the encoding is a proper string and it can be decoded back to that integer. So after decoding you get your ID in database and go retrieve it.

@danboone

This comment has been minimized.

Copy link

commented Jan 7, 2019

update to use string builder and reverse in one step

public string Encode(int i)
        {
            if (i == 0) return Alphabet[0].ToString();

            var s = new StringBuilder();// string.Empty;

            while (i > 0)
            {
                s.Insert(0, Alphabet[i % Base]);
                i = i / Base;
            }

            return  s.ToString();
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.