154

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

5
  • Check out this answer. Jan 3, 2012 at 4:02
  • 14
    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). May 3, 2013 at 8:50
  • possible duplicate of how much bytes will take?
    – nawfal
    Oct 23, 2013 at 7:12
  • @AllonGuralnek:Good point. do you know why diya below didn't suggest to use System.Text.Encoding.Unicode.GetByteCount instead? Why ASCIIEncoding part? Oct 17, 2015 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). Oct 18, 2015 at 4:42

4 Answers 4

181

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);
2
  • 20
    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? Feb 24, 2014 at 1:11
  • 8
    @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.
    – Bob
    Jul 2, 2014 at 2:25
121

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);
5
  • as far as i can understand basics of data structure it's the most 'pined' choise to compare to
    – LoneXcoder
    Oct 8, 2015 at 9:38
  • 5
    Don't forget to take into account the size of the length member. int howManyBytes = yourString.Length * sizeof(Char) + sizeof(int); Nov 29, 2015 at 7:56
  • What if my String length is larger than Int? May 11, 2017 at 6:53
  • 2
    This should be correct answer. Also .Length is what the amount of bytes the server will receive if you send that same string. This is what I was needing.
    – DaWiseguy
    Sep 27, 2019 at 21:41
  • This answer is misleading! It will only correctly calculate the amount of bytes taken by the string in RAM by the application, NOT the amount of bytes a file with that string would have, NOR the length of HTTP content with that string, although it might sometimes give the right answer by coincidence. The real answer is that actual number of bytes depends on chosen encoding. This answer's calculation implies UTF-16, .NET's choice for storing strings in RAM, but hardly used anywhere else, so it will most likely give wrong answers, at least it some cases. Oct 4 at 22:14
29
System.Text.ASCIIEncoding.Unicode.GetByteCount(yourString);

Or

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

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:

using System;
using System.Text;

static void Main()
{
    Encoding testedEncodings = new[]
    {
        Encoding.ASCII,   // Note that '🡪' cannot be encoded in ASCII, data loss will occur
        Encoding.UTF8,    // This should always be your choice nowadays
        Encoding.Unicode, // This is UTF-16. It is used by .NET to store your strings in RAM when the application is running, but this isn't useful information unless you're trying to manipulate bytes in RAM
        Encoding.UTF32
    };

    string text = "a🡪";

    Console.WriteLine($"Tested string: {text}");
    Console.WriteLine($"String length: {text.Length}");
    Console.WriteLine();

    PrintTableHeader("Encoding", "Bytes", "Decoded string");

    foreach (var encoding in testedEncodings)
    {
        byte[] bytes = encoding.GetBytes(text);
        string decodedString = encoding.GetString(bytes);

        PrintTableRow(
            encoding.EncodingName,
            $"{bytes.Length} ({string.Join(' ', bytes)})",
            decodedString);
    }
}

static void PrintTableHeader(params string[] values)
{
    PrintTableRow(values);
    Console.WriteLine(new string('-', 60));
}

static void PrintTableRow(params string[] values)
{
    Console.WriteLine("{0,-16} | {1,-24} | {2}", values);
}

Output:

Tested string: a🡪
String length: 3

Encoding         | Bytes                    | Decoded string
------------------------------------------------------------
US-ASCII         | 3 (97 63 63)             | a??
Unicode (UTF-8)  | 5 (97 240 159 161 170)   | a🡪
Unicode          | 6 (97 0 62 216 106 220)  | a🡪
Unicode (UTF-32) | 8 (97 0 0 0 106 248 1 0) | a🡪

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.