3

I want to find string byte length. Firstly convert to byte and then get the length so How can i get string byte length?

var
  val : String;
begin
  val:= 'example';
  ShowMessage(IntToStr(Length(val) * ???)); -> BYTE LENGTH
end;
2
  • 3
    Or simply use ByteLength if you have enough recent version of Delphi. Commented Mar 31, 2015 at 9:26
  • 1
    i am using xe6, thanks again :) Commented Mar 31, 2015 at 10:04

2 Answers 2

8

You can use the SysUtils.ByteLength() function:

uses
  SysUtils;

var
  val : String;
begin
  val:= 'example';
  ShowMessage(IntToStr(ByteLength(val)));
end;

Just know that ByteLength() only accepts a UnicodeString as input, so any string passed to it, whether that be a (Ansi|Wide|UTF8|RawByte|Unicode)String, will be converted to UTF-16 (if not already) and it will then return the byte count in UTF-16, as simply Length(val) * SizeOf(WideChar).

If you want the byte length of a UnicodeString in another charset, you can use the SysUtils.TEncoding class for that:

var
  val : String;
begin
  val := 'example';
  ShowMessage(IntToStr(TEncoding.UTF8.GetByteCount(val)));
end;

var
  val : String;
  enc : TEncoding;
begin
  val := 'example';
  enc := TEncoding.GetEncoding(...); // codepage number or charset name
  try
    ShowMessage(IntToStr(enc.GetByteCount(val)));
  finally
    enc.Free;
  end;
end;

Or, you can use the AnsiString(N) type to convert a UnicodeString to a specific codepage and then use Length() to get its byte length regardless of what N actually is:

type
  Latin1String = type AnsiString(28591); // can be any codepage supported by the OS...
var
  val : String;
  val2: Latin1String;
begin
  val := 'example';
  val2 := Latin1String(val);
  ShowMessage(IntToStr(Length(val2)));
end;
Sign up to request clarification or add additional context in comments.

Comments

4
var
  val : String;
begin
  val:= 'example';
  ShowMessage(IntToStr(Length(val) * SizeOf(Char)));
end;

Or use ByteLength to obtain the size of a string in bytes. ByteLength calculates the size of the string by multiplying the number of characters in that string to the size of a character.

8 Comments

ByteLength accepts a UTF-16 string, so does funky things if you pass it ANSI or MBCS string.
@DavidHeffernan: Please explain a bit further.
@KromStern: My bet: if you pass anything but a UTF-16 string, you'll first see a conversion to UnicodeString and then get the byte legnth of that, and not of the AnsiString, or UTF8String, or whatever it is you pass.
ByteLength() only accepts a UnicodeString as input, so anything passed to it is converted to UTF-16 (if not already) and it will then return the byte count in UTF-16 (as Length(val) * SizeOf(WideChar)). If you want the byte length of a UnicodeString in another charset, use TEncoding.GetEncoding(CodepageOrCharsetHere) and TEncoding.GetByteCount(UnicodeString). Or use the AnsiString(N) type to convert val to a specific codepage and then use Length(RawByteString) to get its byte length regardless of what N is.
Actually, UTF-16 can use more then two bytes per "character". As usual, Delphi implementation of whatever is lazy and incorrect.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.