0

so i am loading a file that has some encrypted text in it, it uses a custom character table, how can i load it from an external file or put the character table in the code ?

Thank you.

4
  • encrypted or encoded? Also consider showing code that you written to perform conversion... otherwise the question looks like good candidate for closing... Commented Nov 15, 2012 at 1:21
  • @ Ibo: exactly, i already have a character table that works good for me, but i need to implement it or load it directly from the exe/exe directory. @Alex: i have no code, i didn't know how to do it at all :s this is why i asked in the first place. Commented Nov 15, 2012 at 1:21
  • Do you have access to the [custom character table]? Commented Nov 15, 2012 at 1:29
  • yes, here it is: pastebin.com/uUqB2tK4 Commented Nov 15, 2012 at 1:33

1 Answer 1

1

Start by going over the file and counting the lines so you can allocate an array. You could just use a list here but arrays have much better performance and you have a significant amount of items which you'll have to loop over a lot (once for each encoded char in the file) so I think you should use an array instead.

    int lines = 0;
    try 
    {
         using (StreamReader sr = new StreamReader("Encoding.txt")) 
        {
            string line;
            while ((line = sr.ReadLine()) != null) 
            {
                lines++;   
            }
        }
    }
    catch (Exception e) 
    {
        // Let the user know what went wrong.
        Console.WriteLine("The file could not be read:");
        Console.WriteLine(e.Message);
    }

Now we're going to allocate and array of tuples;

 Tuple<string, string> tuples = new Tuple<string, string>[lines];

After that we'll loop over the file again adding each key-value pair as a tuple.

     try 
    {
         using (StreamReader sr = new StreamReader("Encoding.txt")) 
        {
            string line;
            for (int i =0; i < lines; i++) 
            {
                line = sr.Readline();
                if (!line.startsWith('#')) //ignore comments
                {
                     string[] tokens = line.Split('='); //split for key and value
                     foreach(string token in tokens)
                           token.Trim(' '); // remove whitespaces

                    tuples[i].Item1 = tokens[0];
                    tuples[i].Item2 = tokens[1];
                }   
            }
        }
    }
    catch (Exception e) 
    {
        // Let the user know what went wrong.
        Console.WriteLine("The file could not be read:");
        Console.WriteLine(e.Message);
    }

I've given you a lot of code although this may take a little tinkering to make work. I didn't both to write the second loop in a compiler and I'm too lazy to look up things like System.String.Trim and make sure I'm using it correctly. I'll leave those things to you. This has the core logic to do it. If you want to instead use a list move the logic inside of the for loop into the while loop where I count the lines.

Do decode the file you're reading you'll have to loop over this array and compare the keys or values until you have a match.

One other thing - your array of tuples is going to have some empty indexes (the array is of length lines while there are actually lines - comments + blankLines in the file). You'll need some check to make sure you're not accessing these indexes when you try to match characters. Alternatively, you could enhance the file reading so it doesn't count blank lines or comments or remove those lines from the file you read from. The best solution would be to enhance the file reading but that's also the most work.

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

3 Comments

is this only for console ? what about Winforms please ? Thank you.
@Omarrrio No, this is just reading your endcoding file and creating an array of key-value pairs. The idea is to get your character table into memory so it can be used to match characters throughout your application. This works just same for a console app as it does for a winforms app. Once you have the array and you have some string s of custom chars you'll use a nested loop to go over s matching each char with one in the array.
Thank you, and sorry for the late reply.

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.