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;
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;
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.
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.
ByteLengthif you have enough recent version of Delphi.