0

suppose

string a ="bdsbfdsbjfsdjfsdjfjdjdfjbfdgjdfgkjdfjgrytryrtrtydfkgjdfkgdfkjghkdfgj";

string b = encode(a); // b has fixed length of 28 ;

and 

string c = decode(b); 

// c is equal to a .

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
  • 3
    So you want to be able to encode any string with any length into the string of 28 chars? And the decode it back? And your `a` can even be 2^64 chars? Good luck :) – JleruOHeP Feb 25 '14 at 06:06
  • If anyone gets answer to this, we can make ultimate zipper for files. If I get it wright, you want to zip string (long one) to 28 chars? If you just want to create 'alias' for longer string and you can keep longer string in memory, you can create `Dictionary` and translate longer string into shorter one. – Kamil Budziewski Feb 25 '14 at 06:06
  • 7
    If your question is, how do you compress an arbitrary string of arbitrary length to a fixed length, without losing any information, that's not possible. See [Entropy (information theory)](http://en.wikipedia.org/wiki/Entropy_%28information_theory%29#Data_compression) – p.s.w.g Feb 25 '14 at 06:07
  • 1
    Questions like that make me want a new closing reason: "Win me the nobel price by breaking the rules of reality". – TomTom Feb 25 '14 at 06:24

2 Answers2

3

Will a fixed length of 32 do instead?

class MagicEncoder
{
    private Dictionary<string, string> encodedStrings = new private Dictionary<string, string>();

    public string Encode(string input)
    {
        var code = Guid.NewGuid().ToString("N");
        this.encodedStrings[code] = input;
        return code;
    }

    public string Decode(string code)
    {
        string output;
        if (this.encodedString.TryGetValue(code, out output))
        {
             return output;
        }

        throw new ArgumengException("Unknown encoding.");
    }
}

If you really need it to be 28 character, then you can generate unique strings using another method, or use a more compact encoding of a Guid.

If you want to be able to decode your encoded string without using any other data then you are stuck, as already pointed out in the comments.

Community
  • 1
  • 1
Ergwun
  • 12,579
  • 7
  • 56
  • 83
  • I have to encode the string to fixed length and again decode it at another place, I can not pass dictionary element to another place, I have to just pass encoded string. – Ramashanker Tripathi Feb 25 '14 at 06:26
  • I want to use a fixed algorithm to encode it to fixed length and the decode it to get back original. – Ramashanker Tripathi Feb 25 '14 at 06:29
  • 1
    @RamashankerTripathi Yeah, I guessed that's what you're after, but couldn't resist offering this solution, since this was the only interpretation of your question that wasn't impossible :) – Ergwun Feb 25 '14 at 13:26
1

That cannot work? When your reduce the length, information is lost.

The only thing possible is that you decide not to use the full spectrum of UTF-8. Lets assume you only you 7bit ASCII, This means the first 128 chars from the ASCII table. But that only make a length of 7 from a string of length 8. Maybe a bit more if you take into account the full power of UTF-8.

But in the end, if your input string has not a maxed size and encoding, this will not work as you could always get a string longer than the maximum your algorithm supported.

ZoolWay
  • 5,411
  • 6
  • 42
  • 76