none
How to know the byte size of a string? RRS feed

  • Question

  • Hello Guys,

    If the string strSubjectLine need to be only 128 byte long.

    How could I tell the size of the strSubjectLine in Bytes, so I can validate the string and if

    it is longer that 128 bytes send an message to the user?????

    Thanks, PRJUANL

    Tuesday, June 27, 2006 11:31 PM

Answers

  •  BioSlayer wrote:

    C# uses Unicode which is 2 bytes per character so if the limit is 128 bytes you can have 64 chars. You can tell the number of characters in a string by the Length property. You can use other encoding like ASCII to get a character per byte by using the System.Text.Encoding class.

    Or you can use this one :
    System.Text.ASCIIEncoding.Unicode.GetByteCount(s);
    System.Text.ASCIIEncoding.ASCII.GetByteCount(s);

    Depending on the encoding you intend to use.

    Wednesday, June 28, 2006 7:40 AM

All replies

  • C# uses Unicode which is 2 bytes per character so if the limit is 128 bytes you can have 64 chars. You can tell the number of characters in a string by the Length property. You can use other encoding like ASCII to get a character per byte by using the System.Text.Encoding class.

    Wednesday, June 28, 2006 12:11 AM
  •  BioSlayer wrote:

    C# uses Unicode which is 2 bytes per character so if the limit is 128 bytes you can have 64 chars. You can tell the number of characters in a string by the Length property. You can use other encoding like ASCII to get a character per byte by using the System.Text.Encoding class.

    Or you can use this one :
    System.Text.ASCIIEncoding.Unicode.GetByteCount(s);
    System.Text.ASCIIEncoding.ASCII.GetByteCount(s);

    Depending on the encoding you intend to use.

    Wednesday, June 28, 2006 7:40 AM