Thursday, November 15, 2007

Unicode: SizeOf is Different than Length Part II

One thing I forgot to mention in my last unicode post about SizeOf and Length. You can specify the size in bytes of a buffer two ways correctly:


var
ByteCount: Integer;
Buffer: array[0..255] of Char;
begin
ByteCount := SizeOf(Buffer); // Version 1
ByteCount := Length(Buffer) * SizeOf(Char); // Version 2
ByteCount := Length(Buffer) * SizeOf(Buffer[1]); // Version 2 with more clarity
end;


I suggest just going with the first version.

2 comments:

Craig said...

Well, this implies that Length returns the "size" in code units, not code points. Which is fine, but it's going to confuse people even if spelled out in Help.

Chris Bensen said...

Craig,

Honestly I just posted that to be thorough and since there was a comment about prefering to use Length because SizeOf is a little unclear. So this version might be more clear to some.

Post a Comment