Loading ...

How much bytes it will take??

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » How much bytes it will take??

How much bytes it will take??

Posts under the topic: How much bytes it will take??

Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Hello experts,

can anyone tell me below string how much bytes will take?

string abc = "a";

 

Thanks in advance:)


Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered
int size = System.Text.ASCIIEncoding.ASCII.GetByteCount("a");

Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Vinz said:

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???


Posted: 10/19/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

From http://www.yoda.arachsys.com/csharp/strings.html

Memory usage

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.


tags c#

Posted: 10/19/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

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.


Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Confused..Frown


Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

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???

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


Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424

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).

Correct! Since a Unicode encoding takes two bytes for each character.


Posted: 10/19/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
Page 1 of 1 (9 items)