C# equation to Compress string

Hi
I have 35 Character String that I need to convert to 25 Character String
And From 25 Character String i can Return back to 35 Character String
So there is any Math equation can I used to implement this
Im using C# applications
Ameerh24Asked:
Who is Participating?
 
magicdlfConnect With a Mentor Commented:
I made a solution for you. I noticed that it can't be a hex because you have such chars like 'M' and 'G'. Looks like you are using '0' to '9' + 'A' to 'Z' as your source.
It's a pity that I can't reach out an output with readable strings. But the cipher length is exactly 18 chars just as you want.
        Int16 Char2Int(char value)
        {
            if(value>='0' && value<='9')
                return (Int16)(value-'0');
            return (Int16)(char.ToUpper(value) - 'A' + 10);
        }
        char Int2Char(Int16 value)
        {
            if (value < 10)
                return (char)('0' + value);
            return (char)('A' + value - 10);
        }

        public string Encode(string plain)
        {
            plain = "0"+plain.Replace("-", ""); // remove the '-'
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < plain.Length; i+=2)
            {
                char value = (char)((Char2Int(plain[i]) << 8) + Char2Int(plain[i + 1]));
                Int16 v1 = (Int16)(value & 0xff);
                Int16 v2 = (Int16)((value & 0xff00) >> 8);
                sb.Append(value);
            }
            return sb.ToString();
        }

        public string Decode(string cipher)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char ch in cipher)
            {
                Int16 v1 = (Int16)((ch & 0xff00) >> 8);
                Int16 v2 = (Int16)(ch & 0xff);
                sb.Append(Int2Char(v1));
                sb.Append(Int2Char(v2));
            }
            return sb.ToString().Substring(1, 35);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = "FFF80-0EIBB-0CF0F-E6MFG-0A0BB-30221-22009";
            string cipher = Encode(str);
            string plain = Decode(cipher);
            return;
        }

Open in new window

0
 
deepu chandranCommented:
Hi,

Please refer the two compression techniques used in .net
http://msdn.microsoft.com/en-us/library/system.io.compression.aspx

Regards
Deepu
0
 
magicdlfCommented:
Hello, can you please let me know what kind of the string do you have? For example, is it from 'a' to 'z' and 'A' to 'Z'?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Ameerh24Author Commented:

Im Not Mean Normal compression I need to represent 35 Character to 25 Character and if I need to return back to 35 I can this My Question
0
 
Ameerh24Author Commented:
String I have it  Like This
FFF80-0EIBB-0CF0F-E6MFG-0A0BB-30221-22009          (35 Charter)
is To Long so I need to represent in 25 Charter Only but if I need to return back to 35 I can
0
 
stsanzCommented:
> String I have it  Like This
> FFF80-0EIBB-0CF0F-E6MFG-0A0BB-30221-22009  

First convert it to an hexadecimal number. Then each digit will be contained in a half-byte.
This way, you will need only 18 bytes to store it.
0
 
magicdlfCommented:
I agree with stsanz. In addition, are you requesting the encoded string be exact same as 25 chars?
0
 
i-CONICACommented:
stsanz is correct.

But if 25 is absolutely necessary, then append or prepend a fixed, static string to make up the extra characters.
0
 
stsanzCommented:
>> I noticed that it can't be a hex because you have such chars like 'M' and 'G'

hehe, true. I read it too fast ;)
0
 
Ameerh24Author Commented:
Thanks  magicdlf
And also for each one answered my question
but after I using magicdlf code to Covert my String  it Give
        
How I can typed using  Keyword
So I need to Converted it to Valid Character (Keyword Character)
0
 
magicdlfCommented:
If we compress 2 chars into 1, there are 36*36 (0-9,A-Z) states. I don't think you can have 1296 valid chars for a keyword. I think this is a design problem. Please let us know why you must transfer 25 length strings in this case.
0
 
tliottaCommented:
In order to provide an answer (if any exist), we need to know two things:

1. What is the set of characters that are allowed in the 35-character strings?
2. What is the set of characters that are allowed in the 25-character strings?

Until those are known for certain, all answers are essentially random guesses. If the two sets of characters are equal, then the question has no valid answer.

Tom
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.