CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2007
    Posts
    28

    Best way to parse a large string

    Hi, im pretty new to C# and i need your opinions on whats the best ( efficient and professional way to the below function )

    I have a large message ( lets say, 58 charactaters coming over TCP/IP once every second, they are temperature values )

    i have also the same number of custom controls ( lets say labels ).

    my GUI displays such values, and updates everysecond.

    I know its a simple parsing, thats why i need your opinions on how to tackle this.

    1) Should i convert the message into a large string so i can access the members by its array index?

    2) Should i create Class and parse all those numbers to variable members within the class

    3) How about the mapping? should i hard code the mapping, or should i read the mapping ( char[0] = temp1, char[2] = temp 3, etc etc ) from a file or external source.

    4) should i write this function to run on its own thread?


    Any other ideas? any input would be greatly appreciated. This is for work and i dont want to come out as rookish at the peers code review meeting.

    Thanks!

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Best way to parse a large string

    Firstly, a large message would really be classified as thousands/tens of thousands of bytes, 58 bytes is nothing!

    2) Should i create Class and parse all those numbers to variable members within the class

    Would probably be the best option.

    3) How about the mapping? should i hard code the mapping, or should i read the mapping ( char[0] = temp1, char[2] = temp 3, etc etc ) from a file or external source.

    The mapping should be predefined. The exact nature of what you send over the TCP socket should be known at both the receiver and sender.

    4) should i write this function to run on its own thread?
    Probably no need. It's not a terribly complex piece of code. Using asynchronous sockets may be the best option to avoid thread blocking.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Best way to parse a large string

    Seconding Mutant_Fruits opinion (but with an addition )

    When dealing with managed objects (which is every object in C#), the "magic" value for Large is exactly 85,000 BYTES.

    Objects equal to or graeater than this size are treated differently and require special attention [do searches on "Large Object Heap" for details].

    So your message is (almost) exactly 0.068235% the size of a large object. Or looked at another way, your would need 1465.517 of your messages strung together to be "large".
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Feb 2007
    Posts
    28

    Post Re: Best way to parse a large string

    My bad. Does TINY string work?

    Heres another problem i have tho

    This is how i am getting the string.

    Code:
    NetworkStream stream = client.GetStream();
    
                        int i;
                        // Loop to receive all the data sent by the client.
                        try
                        {
                            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                            {
                                // Translate data bytes to a ASCII string.
                                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
    This gives me say, 1 2 3 4 5 6 7 8 10 .. and so on.

    The problem with this is that the values range from 0-1000. So one byte can be 1, 20, 500, and so on. This leaves me guessin where the cutoff of then next value is ( whereis 1 2 3 4, or 12,34 or 1,23,4 ), etc

    What would be a good approach to solve that?
    Is there a way to read one byte at a time?

  5. #5
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Best way to parse a large string

    separate them with commas perhaps? or another delimeter

  6. #6
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Best way to parse a large string

    I believe that string is not the datatype you should use. If I understand your first post you will get messages from a stream. And a message contains an array of character. Each character defines one value, right? So do not use string and use char[] instead. I hope you are able to separate one message from another. So read one whole message and split first the message into the different parts and cast then the values to string for displaying in Controls.
    Useful or not? Rate my posting. Thanks.

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: Best way to parse a large string

    Basically, you need a protocol to describe how you send the data.

    Something along the lines of:
    <int><int><int><string><int> etc...

    First int: Length of entire message
    Second int: Average Temperature
    Third int: Length of next string
    String: Name of a city
    Fourth Int: Temp in that city.

    Etc.

    You can recover each int by reading 4 bytes from the stream, you can decode the string by reading LENGTH number of ints from the stream (assuming text is ASCII like).
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Best way to parse a large string

    Quote Originally Posted by dvazriel
    The problem with this is that the values range from 0-1000. So one byte can be 1, 20, 500, and so on. This leaves me guessin where the cutoff of then next value is ( whereis 1 2 3 4, or 12,34 or 1,23,4 ), etc

    What would be a good approach to solve that?
    Is there a way to read one byte at a time?
    Except that a byte is defined as an 8 bit quantity, and can hold 0-255 (unsigned) or -128 to 127 (signed).....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Feb 2007
    Posts
    28

    Unhappy Re: Best way to parse a large string

    Sorry for not being clear enough. I wish i could be more clear if i could actually READ the message that its coming through, but so far the team that is doing that has 0 code written. I coding based on the specs.

    Basically im NOT sending anything. Im just receiving the message, parse it and display it. The message does have an END and START bytes.

    I will be receiving 16 bit unsigned its, 8 bit packed booleans, etc.

    My problem is that is how can i know when a value end, and a new starts?
    like the example above ( how can i know its 1 2 3 4 and not 12 34 or 123 4 )

    I guess if i know the first set of values ( lets say 10 ) are 16bit, i can read them 2 byte at a time?

    im pretty confused

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    Re: Best way to parse a large string

    I will be receiving 16 bit unsigned its, 8 bit packed booleans, etc.
    You *cannot* decode the message unless you know how it's constructed. However, reading out ints and suchlike is easy. Just use a binary reader on your stream to read the stuff out in int form. Alternatively, just read the complete message out as a byte[] and then use BitConverter to get your int's and suchlike out of that.

    To get the 8 bit packed booleans, just use the reverse of the method used to create the packed booleans.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured