-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;
    }
6
  • What error are you getting? Commented Apr 17, 2014 at 17:58
  • A probable duplicate of stackoverflow.com/questions/1192732/… Commented Apr 17, 2014 at 17:59
  • You may look into something like google.com/search?q=c%23+zip+algorithm Commented Apr 17, 2014 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? Commented Apr 17, 2014 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 holds new tools for "zipping". Commented Apr 18, 2014 at 4:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.