Posted: 10/19/2010
Hello experts,
can anyone tell me below string how much bytes will take?
string abc = "a";
Thanks in advance:)
Vinz said: user="Vincent Maverick Durano"]int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("a");
user="Vincent Maverick Durano"]int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("a");
Thanks for reply Vinz.
int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("a");
it gives me size = 1.
Is it mean it takes 1 byes?
int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("abcd");
it gives me size = 4.
Is it mean it takes 4 bytes?
Am I right Vinz???
From http://www.yoda.arachsys.com/csharp/strings.html
In the current implementation at least, strings take up 20+(n/2)*4 bytes (rounding the value of n/2 down), where n is the number of characters in the string. The string type is unusual in that the size of the object itself varies. The only other classes which do this (as far as I know) are arrays. Essentially, a string is a character array in memory, plus the length of the array and the length of the string (in characters). The length of the array isn't always the same as the length in characters, as strings can be "over-allocated" within mscorlib.dll, to make building them up easier. (StringBuilder does this, for instance.) While strings are immutable to the outside world, code within mscorlib can change the contents, so StringBuilder creates a string with a larger internal character array than the current contents requires, then appends to that string until the character array is no longer big enough to cope, at which point it creates a new string with a larger array. The string length member also contains a flag in its top bit to say whether or not the string contains any non-ASCII characters. This allows for extra optimisation in some cases.
StringBuilder
Mohit, and what if you try like this:
int size = System.Text.ASCIIEncoding.Unicode.GetByteCount("Хајан"); Response.Write(size);
and compare the Vinz's solution as well as the explanation provided by Raghav ;).
In other words, if your string contains ASCII characters, you can simply get the string.Length and thats the size of the string in bytes, since each ASCII character is equal to one byte (AFAIK), however, if you have non-ASCII characters, its not the same, so as in my example, the result will be 10 (not 5).
Hope this helps.
Confused..
mohit said: user="mohit kumar"]Thanks for reply Vinz.int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("a");it gives me size = 1.Is it mean it takes 1 byes?int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("abcd");it gives me size = 4.Is it mean it takes 4 bytes?Am I right Vinz???
user="mohit kumar"]
Yes because AFAIK an ASCII always take up one byte per character but please note that this not always be the case, getting the size of a string depends on the encoding type you use. In .NET it uses Unicode instead of ASCII. So with Unicode encoding a string takes two bytes in memory.
int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("a"); // 1
int size = System.Text.ASCIIEncoding.Unicode.GetByteCount("a"); // 2
hajan said: user="Hajan Selmani"]In other words, if your string contains ASCII characters, you can simply get the string.Length and thats the size of the string in bytes, since each ASCII character is equal to one byte (AFAIK), however, if you have non-ASCII characters, its not the same, so as in my example, the result will be 10 (not 5).
user="Hajan Selmani"]In other words, if your string contains ASCII characters, you can simply get the string.Length and thats the size of the string in bytes, since each ASCII character is equal to one byte (AFAIK), however, if you have non-ASCII characters, its not the same, so as in my example, the result will be 10 (not 5).
Correct! Since a Unicode encoding takes two bytes for each character.
.NET follows UTF-16 http://en.wikipedia.org/wiki/UTF-16/UCS-2