138

I'm wondering if I can know how long in bytes for a string in C#, anyone know?

Majid
  • 13,097
  • 15
  • 74
  • 110
user705414
  • 19,158
  • 37
  • 109
  • 154
  • Check out [this answer](http://stackoverflow.com/questions/472906/net-string-to-byte-array-c-sharp). – Sergey Kalinichenko Jan 03 '12 at 04:02
  • 13
    Are you asking how much memory a `string` object occupies, or how many bytes the representation of a string will occupy when written to a file or sent over a network (i.e. encoded), because those are two completely different questions. majidgeek almost answered the former while diya answered the latter (at least for two common encodings). – Allon Guralnek May 03 '13 at 08:50
  • possible duplicate of [how much bytes will take?](http://stackoverflow.com/questions/3967411/how-much-bytes-will-take) – nawfal Oct 23 '13 at 07:12
  • @AllonGuralnek:Good point. do you know why diya below didn't suggest to use System.Text.Encoding.Unicode.GetByteCount instead? Why ASCIIEncoding part? – Giorgi Moniava Oct 17 '15 at 19:55
  • @Giorgi: Since `Unicode` is a static property of `System.Text.Encoding`, which is the base class of `ASCIIEncoding`, both statements are actually the same. You can access a static member from subclasses as well (but it's not considered idiomatic). – Allon Guralnek Oct 18 '15 at 04:42

5 Answers5

164

You can use encoding like ASCII to get a character per byte by using the System.Text.Encoding class.

or try this

  System.Text.ASCIIEncoding.Unicode.GetByteCount(string);
  System.Text.ASCIIEncoding.ASCII.GetByteCount(string);
diya
  • 6,728
  • 9
  • 36
  • 55
  • 17
    Stupid question, but how will we know whether to use the Unicode or ASCII class if the data in the string came from a 3rd party file? – Matthew Lock Feb 24 '14 at 01:11
  • 7
    @MatthewLock You should use UTF16 (or majidgeek's `Length * sizeof(Char)`, which should give the same result since each `Char` is UTF16/2-bytes) if you want the same number of bytes as the internal representation of a string. If you actually want the exact amount of memory the entire object takes, rather than just the number of bytes in its internal character array, then you might consider a [more general method](http://stackoverflow.com/questions/1128315/find-size-of-object-instance-in-bytes-in-c-sharp). – Bob Jul 02 '14 at 02:25
112

From MSDN:

A String object is a sequential collection of System.Char objects that represent a string.

So you can use this:

var howManyBytes = yourString.Length * sizeof(Char);
Majid
  • 13,097
  • 15
  • 74
  • 110
27
System.Text.ASCIIEncoding.Unicode.GetByteCount(yourString);

Or

System.Text.ASCIIEncoding.ASCII.GetByteCount(yourString);
2

How many bytes a string will take depends on the encoding you choose (or is automatically chosen in the background without your knowledge). This sample code shows the difference:

void Main()
{
    string text = "a";
    Console.WriteLine("{0,15} length: {1}", "String", text.Length);

    PrintInfo(text, Encoding.ASCII); // Note that '' cannot be encoded in ASCII, information loss will occur
    PrintInfo(text, Encoding.UTF8); // This should always be your choice nowadays
    PrintInfo(text, Encoding.Unicode);
    PrintInfo(text, Encoding.UTF32);
}

void PrintInfo(string input, Encoding encoding)
{
    byte[] bytes = encoding.GetBytes(input);

    var info = new StringBuilder();
    info.AppendFormat("{0,16} bytes: {1} (", encoding.EncodingName, bytes.Length);
    info.AppendJoin(' ', bytes);
    info.Append(')');

    string decodedString = encoding.GetString(bytes);
    info.AppendFormat(", decoded string: \"{0}\"", decodedString);

    Console.WriteLine(info.ToString());
}

Output:

         String length: 3
        US-ASCII bytes: 3 (97 63 63), decoded string: "a??"
 Unicode (UTF-8) bytes: 5 (97 240 159 161 170), decoded string: "a"
         Unicode bytes: 6 (97 0 62 216 106 220), decoded string: "a"
Unicode (UTF-32) bytes: 8 (97 0 0 0 106 248 1 0), decoded string: "a"
Robert Synoradzki
  • 1,498
  • 12
  • 18
-3

Starting with .Net5, you can use Convert.ToHexString. There's also a method for the reverse operation: Convert.FromHexString.

Sebastian
  • 15
  • 2