none
Determining Size of List<MyStruct> RRS feed

  • Question

  • I have a list containing structures which contain only strings. I need to convert this into binary to send down the wire? How can I determine how many bytes are needed?
    Wednesday, November 16, 2011 1:47 PM

Answers

All replies

  • Why are you worried about number of bytes. If you are using binary stream, you can use standard values like 256, 512, 1024 etc. You just need to transmit the data and the system will take of it automatically.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, November 16, 2011 2:17 PM
  • Try using Linq. Here is an example:

    List<string> list = new List<string> { "a", "b", "c" };
    byte[] bytes = list.SelectMany(s => Encoding.UTF8.GetBytes(s)).ToArray();
    



    Mitja
    Wednesday, November 16, 2011 2:25 PM
  • Ok, well for example, I have a Data class that contains the data to send. This holds things like a string containing a message, a name, etc... I determine the length of the each string such as the message in the data class and assign a byte for each character within the string. When it's all packed up I convert the class to an array of bytes and send it on it's way. That works fine, but now I added a list of structures in my Data class and I'm trying to check the size of it so I know how to pack it up... I'm not understanding what you are trying to tell me, please explain.

     

    Here is the code...

     

    public enum CommandOp
        {
            SignIn,
            SignOut,
            GetContactList,
            Message, 
            SendFile,
            Null
        }
    
        public class Data
        {
            public CommandOp Command;
            public bool Authenticated;  // To be added...
            public List<Contact> Contacts; // To be added...
            public string UserIdFrom;        
            public string DisplayName;
            public string UserIdTo;        
            public string Message;        
    
            public Data()
            {
                this.Command = CommandOp.Null;
                this.UserIdFrom = null;
                this.DisplayName = null;
                this.UserIdTo = null;            
                this.Message = null; 
            }
    
    
            public Data(byte[] data)
            {
                this.Command = (CommandOp)BitConverter.ToInt32(data, 0);
                int fromUserIdLength = BitConverter.ToInt32(data, 4);
                int displayNameLength = BitConverter.ToInt32(data, 8);
                int toUserIdLength = BitConverter.ToInt32(data, 12);
                int messageLength = BitConverter.ToInt32(data, 16);
    
                if (fromUserIdLength > 0)
                    this.UserIdFrom = Encoding.UTF8.GetString(data, 20, fromUserIdLength);
                else
                    this.UserIdFrom = null;
    
                if (displayNameLength > 0)
                    this.DisplayName = Encoding.UTF8.GetString(data, 20 + fromUserIdLength, displayNameLength);
                else
                    this.UserIdTo = null;
    
                if (toUserIdLength > 0)
                    this.UserIdTo = Encoding.UTF8.GetString(data, 20 + fromUserIdLength + displayNameLength, toUserIdLength);
                else
                    this.UserIdTo = null;            
    
                if (messageLength > 0)
                    this.Message = Encoding.UTF8.GetString(data, 20 + fromUserIdLength + displayNameLength + toUserIdLength, messageLength);
                else
                    this.Message = null;
            }
    
            public byte[] ToByte()
            {
                List<byte> result = new List<byte>();
    
                result.AddRange(BitConverter.GetBytes((int)Command));
    
                if (UserIdFrom != null)
                    result.AddRange(BitConverter.GetBytes(UserIdFrom.Length));
                else
                    result.AddRange(BitConverter.GetBytes(0));
    
                if (DisplayName != null)
                    result.AddRange(BitConverter.GetBytes(DisplayName.Length));
                else
                    result.AddRange(BitConverter.GetBytes(0));
    
                if (UserIdTo != null)
                    result.AddRange(BitConverter.GetBytes(UserIdTo.Length));
                else
                    result.AddRange(BitConverter.GetBytes(0));
    
                if (Message != null)
                    result.AddRange(BitConverter.GetBytes(Message.Length));
                else
                    result.AddRange(BitConverter.GetBytes(0));
    
    
    
                if (UserIdFrom != null)
                    result.AddRange(Encoding.UTF8.GetBytes(UserIdFrom));
    
                if (DisplayName != null)
                    result.AddRange(Encoding.UTF8.GetBytes(DisplayName));
    
                if (UserIdTo != null)
                    result.AddRange(Encoding.UTF8.GetBytes(UserIdTo));
    
                if (Message != null)
                    result.AddRange(Encoding.UTF8.GetBytes(Message));
    
                return result.ToArray();
            }
        }


    • Edited by Phenomin Wednesday, November 16, 2011 2:40 PM
    Wednesday, November 16, 2011 2:33 PM
  • What encoding are you going to use for the transmitted strings? The answer to that will affect the total size.
    Wednesday, November 16, 2011 2:34 PM
  • Well as you can see I'm using UTF-8 which, I guess could be more than a byte per character... This will be fixed, but for the time being I'm not expecting any character more than a byte in size.

    So what you are saying though, is that the size of the list is the size of the 2 strings that are held within the list? I just wasn't if the List itself took up some extra space or whatnot, seems like it would... 

    • Edited by Phenomin Wednesday, November 16, 2011 3:13 PM
    Wednesday, November 16, 2011 3:10 PM
  • So what you are saying though, is that the size of the list is the size of the 2 strings that are held within the list? I just wasn't if the List itself took up some extra space or whatnot, seems like it would... 

    The size of the data you send depends on what you send. The simplest, I think, is send the number of strings, then for each string the number of characters followed by each character. In that case, "the list itself" takes the size you need to send the number of strings.
    Wednesday, November 16, 2011 4:37 PM
  • Ok, well what I'm trying to do is develop a chat application. What happens is, the user types in their login credentials and sends it to the server. The server then checks a database and then if the credentials pass, a structure containing the contact id and contact display name is created for each contact (after it compares it with users logged into the server),  and then stored into a generic list. The list is then added to the data class which holds all the info to be converted and sent down the wire... 

    I don't know if it sounds overly complicated, but it's an idea that would work if I can figure it out. If there are any more efficient/easier ways (of course there is), I'm widely open to suggestions.


    • Edited by Phenomin Wednesday, November 16, 2011 6:18 PM
    Wednesday, November 16, 2011 6:18 PM
  • Hi Phenomin,

    After researching, I find some articles may help you:

    TCP/IP Chat Application using C#

    Basic Client/Server Chat Application in C#

    A Chat Client/Server Program

    I hope these information can help you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 21, 2011 6:13 AM
    Moderator
  • Thanks, I'm glanced at these before but I think I'll look a little more into it.
    Monday, November 21, 2011 4:00 PM
  • Hi Phenomin,

    I marked Louis's and my reply as answer, if you feel the replys are not answers, please unmark them. If you have any problem, please feel free to post in the forum, we'll do our best to help you to solve the issue.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, November 22, 2011 5:51 AM
    Moderator