10 Replies - 9030 Views - Last Post: 16 September 2010 - 12:01 PM Rate Topic: -----

#1 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

C# Convert string into multidimensional array

Posted 16 September 2010 - 08:21 AM

Hello, this week is the first time I've ever touched C# (Visual C#), I've worked my way through a beginners book and many online tutorials and got myself at the stage where I feel comfortable with the basics of the language. The problem I am finding is I can't seem to find answers to simple questions without getting in a mess.

One problem I have now which I can't find any advice on it is converting a string into a multidimensional array. Below is my code and how I want it to work...

        public string queryServerURI(string uri)
        {
            WebClient wc = new WebClient();
            byte[] data =
                wc.DownloadData("http://***.***.**.*/*****/adminconsole.php?user=" + username + "&pass=" + password + uri);
            string response = Encoding.ASCII.GetString(data);
            return response;
        }


(username + password are declared previously in the code)
This first bit of code is my server query method, the PHP page it's pulling a string from works fine and returns data how needed. I call this function like so (for the problem)...

    queryServerURI("&query=SELECT%S*%SFROM%SUsers);



The PHP script then returns...

19%FKruithne%Fcensored%Fcensored%Fcensored%F1%F1%F0%F32777%F1%F0%F%F500%F0%F0%F1%F%F#00FF00%F#0000FF%F%F%F0%F%R20%FBoogers%Fcensored%Fcensored%Ftest@test.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R21%FTester%Fcensored%Fcensored%Ftest2@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R22%FTesterzx%Fcensored%Fcensored%Ftest3@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R23%FTestex%Fcensored%Fcensored%Ftest43@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F



%F = divides fields
%R = divides rows
Table has 23 cols and 5 rows.

The way I have tried to get this to work is by splitting the rows into a normal array ...

    string response = queryServerURI("&query=SELECT%S*%SFROM%SUsers);
    string[] rows = Regex.Split(response, "%R");



As far as I can see this does not work and I'm unsure why/how to check. I tried to print rows[0] into a text field and nothing happened.
The below code is just a rough plan of what I am trying to achieve with the entire thing but I am very unsure of how to set this out and what to use.


    string response = queryServerURI("&query=SELECT%S*%SFROM%SUsers);
    string[] rows = Regex.Split(response, "%R"); // splits rows into normal array

    string[,] results; //  creates multidimensional array to hold it all

    foreach(string row in rows) // gets the non-split fields for each array ...
    {

        string[] fields = Regex.Split(row, "%F"); // makes normal array with the fields
        fields.CopyTo(results, 0); // this is where im lost(er) .. want to apply the fields to the multid array so it's like {{fields},{fields},{fields}}
        // something like results = new string[,]{ fields } dosn't work either.

    }



If anyone could help me out or point me in the right direction of a code to make what i'm trying to achieve easier I would be really grateful! If you need anymore information about my code please ask below. Thanks in advance! :)

Is This A Good Question/Topic? 0
  • +

Replies To: C# Convert string into multidimensional array

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 08:27 AM

Moved to C#.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 08:45 AM

try this and see if it gives you what you want.

string[] rows = data.Split(new string[]{"%R"}, StringSplitOptions.None);
string[,] results = new string[rows.Length,23];

for (int i = 0; i < rows.Length; i++)
{
    string[] fields = rows[i].Split(new string[] { "%F" }, StringSplitOptions.None);

    for (int j = 0; j < fields.Length; j++)
    {
        results[i, j] = fields[j];
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 08:58 AM

Sorry for posting in the wrong section - didn't notice I did.

View Posteclipsed4utoo, on 16 September 2010 - 07:45 AM, said:

try this and see if it gives you what you want.


Put this in place of my code ...

            string data = queryServerURI("&query=SELECT%S*%SFROM%SUsers");
            string[] rows = data.Split(new string[]{"%R"}, StringSplitOptions.None);
            string[,] results = new string[rows.Length,23];

            for (int i = 0; i < rows.Length; i++)
            {
                string[] fields = rows[i].Split(new string[] { "%F" }, StringSplitOptions.None);

                for (int j = 0; j < fields.Length; j++)
                {
                    results[i, j] = fields[j];
                 }
            }

            quizData_id.Text = results[1, 0]; //to check



Got an error "Index was out of array bounds" when trying to put results[1, 0] into a text field to check.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 09:58 AM

the most likely, you only received one "record" from the server. You should definitely put a check in before trying to access an element of an array.

// checks to make sure there are atleast 2 rows in the array.
if (rows.Count > 1)
    quizData_id.Text = results[1, 0];


Was This Post Helpful? 0
  • +
  • -

#6 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 10:00 AM

When I preview that query on the actual page the response from the server is ...

19%FKruithne%Fcensored%Fcensored%Fcensored%F1%F1%F0%F32777%F1%F0%F%F500%F0%F0%F1%F%F#00FF00%F#0000FF%F%F%F0%F%R20%FBoogers%Fcensored%Fcensored%Ftest@test.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R21%FTester%Fcensored%Fcensored%Ftest2@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R22%FTesterzx%Fcensored%Fcensored%Ftest3@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R23%FTestex%Fcensored%Fcensored%Ftest43@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F



This contains 5 rows of data in it. The above code you gave me changes nothing when applied. :(
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 10:15 AM

I run this code...

string data = "19%FKruithne%Fcensored%Fcensored%Fcensored%F1%F1%F0%F32777%F1%F0%F%F500%F0%F0%F1%F%F#00FF00%F#0000FF%F%F%F0%F%R20%FBoogers%Fcensored%Fcensored%Ftest@test.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R21%FTester%Fcensored%Fcensored%Ftest2@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R22%FTesterzx%Fcensored%Fcensored%Ftest3@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F%R23%FTestex%Fcensored%Fcensored%Ftest43@hotm.com%F0%F%F%F0%F0%F0%F%F0%F0%F0%F0%F%F#00FF00%F#0000FF%F%F%F0%F";

string[] rows = data.Split(new string[]{"%R"}, StringSplitOptions.None);
string[,] results = new string[rows.Length,23];

for (int i = 0; i < rows.Length; i++)
{
    string[] fields = rows[i].Split(new string[] { "%F" }, StringSplitOptions.None);

    for (int j = 0; j < fields.Length; j++)
    {
        results[i, j] = fields[j];
    }
}

Console.Write(results[1, 0]);
Console.Read();



and the output is 20.
Was This Post Helpful? 0
  • +
  • -

#8 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 10:45 AM

I see, I just ran your code using the data without the server query and it seems to work fine on my end as well. When I go to the server URL that "should" be being used, the printed output is fine. For some reason the C# is not getting the same, or any, response. Is there anything wrong with this ...


        public static string username;
        public static string password;

public string queryServerURI(string uri)
        {
            WebClient wc = new WebClient();
            byte[] data =
                wc.DownloadData("http://***.***.**.*/*****/adminconsole.php?user=" + username + "&pass=" + password + uri);
            string response = Encoding.ASCII.GetString(data);
            return response;
        }



Other than the censored URL :)

The username and password values are filled in from a LoginPage form. When they are printed out to a text field they both contain correct information.
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 10:59 AM

Is the byte array blank? Like absolutely nothing is returned?
Was This Post Helpful? 0
  • +
  • -

#10 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 12:00 PM

Ahhhhh doh. The problem was with the URI of the server query, it was missing the &a=request so the server was taking it as a login query rather than a data query, hense why I didn't spot the error on the server side of things. All sorted now and your string => multidimensional array works a charm! Thank you very much for helping me out, probably would have ended up with some very long winded conversion code by the time I had finished. :)
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Convert string into multidimensional array

Posted 16 September 2010 - 12:01 PM

no problem. glad to see you figured out that issue.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1