Subject | Re: udf firebird delphi |
---|---|
Author | Steve Summers |
Post date | 2004-03-31T19:02:03Z |
--- In firebird-support@yahoogroups.com, Marc Collin <os2@v...> wrote:
You can't just do that. Delphi strings use 1 as the origin. PChars
use 0, so Length(PCharStr) actually returns an index ONE BEYOND the
length of your string- i.e., the position of the null that terminates
it.
You need to change your FOR loop to go from Length(Str)-1 downto 0.
> in delphi i use:In your UDF version, you're substituting PChar for Delphi Strings.
>
> Function TForm1.MixStr(Str:String):String;
> var
> i, j : Integer;
> c1 : Char;
> Begin
> For i:=Length(Str) downto 1 Do
> Begin
> j:=Random(i)+1;
> c1:=Str[i];
> Str[i]:=Str[j];
> Str[j]:=c1;
> End;
> result := Str;
> End;
>
> but can't use that for udf...
>
You can't just do that. Delphi strings use 1 as the origin. PChars
use 0, so Length(PCharStr) actually returns an index ONE BEYOND the
length of your string- i.e., the position of the null that terminates
it.
You need to change your FOR loop to go from Length(Str)-1 downto 0.