-1

For Example Make "hashdkjhs654asdkjlsdakjhakjhkkajdssdsa" to be "Gf5i"

I tried this ways But fail.

private void encodeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        string textstring;
        string encodedString;
        textstring = richTextBox1.Text;
        byte[] encodedBytes = utf8.GetBytes(textstring);
        richTextBox1.Clear();
        encodedBytes.ToString(encodedString);
        richTextBox1.Text = encodedString;
    }
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
user3225402
  • 25
  • 2
  • 3
  • What error are you getting? – mituw16 Apr 17 '14 at 17:58
  • A probable duplicate of http://stackoverflow.com/questions/1192732/really-simple-short-string-compression – memoryz Apr 17 '14 at 17:59
  • You may look into something like https://www.google.com/search?q=c%23+zip+algorithm – Yuriy Galanter Apr 17 '14 at 18:04
  • From the phrasing of your question I'm not certain that you're looking for a "compression" algorithm necessarily. What's the purpose of doing this in the first place? – Vasily Sliounaiev Apr 17 '14 at 21:16
  • Not clear what you are asking, but since .NET 4.5 (2012), if you refer `System.IO.Compression.dll` the [namespace `System.IO.Compression`](http://msdn.microsoft.com/en-us/library/system.io.compression.aspx) holds new tools for "zipping". – Jeppe Stig Nielsen Apr 18 '14 at 04:53
  • What exactly are your requirements? The pigeonhole principle says that there is no way to compress all strings of the length of your example to four character strings and have it still be reversable. If you tell us what you want to do we can probably help more... – Chris Apr 18 '14 at 09:35

1 Answers1

1

What you want to do is writing an algorithm that compresses strings. However, this isn't really something you can do by just snapping your fingers.

You should first consider the type of raw input you're receiving: is it humanly comprehensible text? Then my advice is to use a pattern-searching algorithm, which looks for repeating sequences of characters and marks their position, using instead their coordinates from that point on. That's what most file-compression algorithms, like WinRar's, mainly do. To decode you then iterate the compressed string, look for coordinate headers and use them to re-ensemble the whole thing.

EDIT: Also, your solution is wrong because the string parameter for byte[].ToString(string) isn't the variable you want to assign, but rather a format. You should do something on the lines of:

private void encodeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        string textstring;
        string encodedString;
        textstring = richTextBox1.Text;
        byte[] encodedBytes = utf8.GetBytes(textstring);
        richTextBox1.Clear();
        encodedString = encodedBytes.ToString();
        richTextBox1.Text = encodedString;
    }

But I doubt that will make the string significantly shorter.

KappaG3
  • 660
  • 5
  • 14