Re:Strings & EOutOfMemory
Quote
"Murdoc" <murdo...@hotmail.com> wrote in message news:ah7ti4
> for I := 1 to Bits do
> if Number > Power(2,I) then
> begin
> BinString[Bits-I] := '1';
> Number := Number - Floor(Power(2,I));
> end
> else
> BinString[Bits-I] := '0';
The index calculation into BinString is returning a value out of the range
of the string. I suspect that you are running without range checking
enabled. Among other things, the code is clobbering memory.
function IntToBin (num, bits : integer) : string;
var lth, i, lastSet, argSize : integer;
begin
argSize := SizeOf (num) * 8;
lth := Max (argSize, bits);
result := StringOfChar ('0', lth);
lastSet := lth;
for i := lth downto Max (1, lth - argSize) do
begin
if (num and $01) > 0
then begin
result [i] := '1';
lastSet := i;
end;
num := num shr 1;
end;
if (lth - lastSet) >= bits
then Delete (result, 1, lastSet - 1)
else Delete (result, 1, lth - bits);
end;