user705414
user705414

Reputation: 21200

How to know the size of the string in bytes?

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

Upvotes: 163

Views: 222001

Answers (4)

Robert Synoradzki
Robert Synoradzki

Reputation: 2026

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🡪

Upvotes: 16

Majid
Majid

Reputation: 14243

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

Upvotes: 129

diya
diya

Reputation: 7138

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

Upvotes: 189

user596075
user596075

Reputation:

System.Text.ASCIIEncoding.Unicode.GetByteCount(yourString);

Or

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

Upvotes: 30

Related Questions