0

I'm trying to convert a string of characters into each characters decimal form and seperating them with a symbol that is chosen at random and then after that's converted, seperating the decimal numbers from the string and then adding 1 to those numbers and then converting them back into ASCII characters. Here's what I have so far but it keeps saying invalid input format with 'int.Parse':

public string Encode(string data, out string asciiString) {
            char[] dataArray = data.ToCharArray();
            string[] symb = {"@","#","$","%","&"};
            Random rnd = new Random();
            string newData = "";
            for(int i = 0; i < dataArray.Length; i++) {
                newData += (((int)dataArray[i] + 1) + symb[rnd.Next(0, 5)].ToString()); // add 1 to the decimal and then add symbol
            }
            asciiString = ConvertToAscii(newData);
            return newData;
        }

        public string ConvertToAscii(string data) {
            string[] tokens = data.Split('@', '#', '$', '%', '&');
            data = data.TrimEnd('@', '#', '$', '%', '&');
            string returnString = "";
            for(int i = 0; i < tokens.Length; i++){

                int num = int.Parse(tokens[i]);

                returnString += (char)num;
            }
            return returnString;
        }

Here's an example:

Normal: "Hello" converted to decimal with symbols: 72$101&108#108@111% converted to ascii (without symbols and adding 1): Ifmmp (I had to do it with an ascii table)

4
  • 2
    Could you give us an example? Commented Nov 25, 2011 at 19:47
  • 1
    What is tokens[i] when it coughs up the error? Commented Nov 25, 2011 at 19:48
  • "Hello" converted to decimal with symbols: 72$101&108#108@111% converted to ascii (with seperation and adding 1): Ifmmp Commented Nov 25, 2011 at 19:51
  • Gotta be a homework problem :-) Commented Nov 26, 2011 at 15:11

4 Answers 4

3

You should switch these lines around so you can avoid calling Parse with an empty string.

string[] tokens = data.Split('@', '#', '$', '%', '&');
data = data.TrimEnd('@', '#', '$', '%', '&');
Sign up to request clarification or add additional context in comments.

Comments

3

int.TryParse is almost always the better way to handle parsing to int. It takes two arguments, try replacing:

int num = int.Parse(tokens[i]);

with

int num;
if (!int.TryParse(tokens[i], out num))
{
    Debug.WriteLine(string.Format("'{0}' can't be converted to an int.", tokens[i]));
}

and you will see what is going on with the failed parse.

1 Comment

This led me to realizing it was adding an extra character so I did what ChaosPandion said and it fixed it.
1

The data.Split call contains an empty string (check the tokens result). You can remove empty entries as follows:

string[] tokens = data.Split(new[] {'@', '#', '$', '%', '&'},
                           StringSplitOptions.RemoveEmptyEntries);

Comments

0

Have you tried to use

string[] tokens = data.Split(new char[]{'@', '#', '$', '%', '&'},StringSplitOptions.RemoveEmptyEntries);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.