[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C#  equation to Compress string

Posted on 2009-12-22
12
Medium Priority
?
1,113 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:Ameerh24
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 8

Expert Comment

by:deepu chandran
ID: 26104012
Hi,

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

Regards
Deepu
0
 
LVL 9

Expert Comment

by:magicdlf
ID: 26104133
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
 

Author Comment

by:Ameerh24
ID: 26104140

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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:Ameerh24
ID: 26104210
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
 
LVL 6

Expert Comment

by:stsanz
ID: 26104501
> 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
 
LVL 9

Expert Comment

by:magicdlf
ID: 26104531
I agree with stsanz. In addition, are you requesting the encoded string be exact same as 25 chars?
0
 
LVL 6

Expert Comment

by:i-CONICA
ID: 26104566
stsanz is correct.

But if 25 is absolutely necessary, then append or prepend a fixed, static string to make up the extra characters.
0
 
LVL 9

Accepted Solution

by:
magicdlf earned 2000 total points
ID: 26104821
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
 
LVL 6

Expert Comment

by:stsanz
ID: 26104859
>> 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
 

Author Comment

by:Ameerh24
ID: 26105293
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
 
LVL 9

Expert Comment

by:magicdlf
ID: 26105343
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
 
LVL 27

Expert Comment

by:tliotta
ID: 26116635
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Aerodynamic noise is the cause of the majority of the noise produced by helicopters. The inordinate amount of noise helicopters produce is a major problem in the both a military and civilian setting. To remedy this problem the use of an aerogel coat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question