Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I have a List<string> populated with data. And I want to know how many total bytes it contains.
But this shows an exception "Input string is in incorrect format".
C#
List<string> lll = new List<string>();
            lll.Add("Hello");
            lll.Add("how are you");
            byte sssss = Convert.ToByte(lll);


I don't want to make it in Byte[]. is there any way i can get this?





Thanks :)
Posted

1 solution

You can't just say "I want the size of the whole list" without examining the length of each string in the collection. You can say "How many strings ar in the collection?" very easily:
C#
int count = lll.Count;
But to work out how many bytes are ar in all the strings it contains, takes a little more work.
There are a number of ways to do it, the simplest is an explicit loop:
C#
int count = 0;
foreach (string s in lll)
    {
    count += s.Length;
    }
Console.WriteLine(count);

Or you could use Linq methods:
C#
int count = lll.Sum(s => s.Length);
Console.WriteLine(count);
But that is really just "hiding" the loop!
 
Share this answer
 
Comments
Manish Kumar Namdev 16-May-13 3:02am    
Hi thanks for reply.
Byte[] bts = Program.GetBytes(lll.ToString());
Console.WriteLine(bytes.Length);

static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
In this method it is returning 96

And
int count = lll.Sum(s => s.Length);
Console.WriteLine(count);

This returns 16.

What is happening in the above both codes and why they are showing difference.
Please make me correct and guide me to understand about this difference.

Thanks
MuhammadUSman1 16-May-13 3:09am    
In your case:
you are converting lll.ToString() and taking its bytes mean in your GetBytes method it taking parameter values is "System.Collections.Generic.List`1[System.String]" and taking its byts and return. that is totally invalid.
you should try this.


int count = lll.Sum(s => s.Length); Console.WriteLine(count);
MuhammadUSman1 16-May-13 3:10am    
OriginalGriff ans is correct. follow him.
thanx
OriginalGriff 16-May-13 3:20am    
As MuhammadUsman1 said, when you call ToString on a List of any form, you do not get the content of the List, but instead the name of the class that the List is in.
This is the default operation of object.ToString, and unless it is overridden (as it is for int, double, float, and so forth) that is what you get. This can confuse people, especially when they start using Image or Bitmap classes, and assume that myImage.ToString will give them a string version of the JPG file they loaded. It won't - it gives you "System.Drawing.Bitmap" which isn't a lot of help when you save it to a database... :laugh:
MuhammadUSman1 16-May-13 3:03am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900