suppose
string a ="bdsbfdsbjfsdjfsdjfjdjdfjbfdgjdfgkjdfjgrytryrtrtydfkgjdfkgdfkjghkdfgj";
string b = encode(a); // b has fixed length of 28 ;
and
string c = decode(b);
// c is equal to a .
suppose
string a ="bdsbfdsbjfsdjfsdjfjdjdfjbfdgjdfgkjdfjgrytryrtrtydfkgjdfkgdfkjghkdfgj";
string b = encode(a); // b has fixed length of 28 ;
and
string c = decode(b);
// c is equal to a .
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.
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.