Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

php function to delphi

Hi experts.

I need to convert a JavaScript code to delphi:

controle1 = (Math.random() * 65465 | 0).toString(16)
controle2 = (2765476 * Math.random() | 0).toString(36);

Someone could help me ?

Thanks
0
alpires
Asked:
alpires
  • 2
1 Solution
 
gskoczylasSenior Software DeveloperCommented:
Something like this (not tested this code):
unit Unit1;

interface

implementation

type
  TBaseRange = 2..36;

function  ToString({const} nValue:  Integer;
                   const   nBase:  TBaseRange)
                   : string;

  function  ToChar(const   nDigit:  Integer)
                   : Char;
  begin
    if  nDigit < 10  then
      Result := Chr(Ord('0') + nDigit)
    else
      Result := Chr(Ord('A') + nDigit - 10)
  end { ToChar };

begin  { ToString }
  Assert(nValue >= 0);
  Result := '';
  repeat
    Result := ToChar(nValue mod nBase) + Result;
    nValue := nValue div nBase
  until  nValue = 0
end { ToString };


procedure  Convert;

var
  controle1:  string;
  controle2:  string;

begin  { Convert }
  // controle1 = (Math.random() * 65465 | 0).toString(16)
  controle1 := ToString(Random(65465) {or 0}, 16);

  // controle2 = (2765476 * Math.random() | 0).toString(36);
  controle2 := ToString(Random(2765476) { or 0}, 36);
end { Convert };

initialization
  Randomize();

end.

Open in new window

0
 
rinfoCommented:
Function BaseChange(Num, NewBase : Word) : String;
Const
  BaseChars : Array [0..36] of Char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Var
  St : String;
begin
  St := '';
  Repeat
    St  := BaseChars[Num MOD NewBase] + St;
    Num := Num Div NewBase;
  Until Num = 0;
  BaseChange := St;
end;

Int var1 = BaseChange((1+Random(65465),16);
if var1 then Control1:= IntTostr(var1) else control1:= '0';
Int var1 = BaseChange(floor((2765476*Random)),36);
if var1 then control2 := IntTostr(var1) else control2 = '0';
0
 
rinfoCommented:
This code is tested
Function TForm1.BaseChange(Num, NewBase : Word) : String;
Const
  BaseChars : Array [0..36] of Char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Var
  St : String;
begin
  St := '';
  Repeat
    St  := BaseChars[Num MOD NewBase] + St;
    Num := Num Div NewBase;
  Until Num = 0;
  BaseChange := St;
end;


procedure TForm1.Button1Click(Sender: TObject);
var var1:string;Control1:String;
    num:integer;
begin

 var1:= BaseChange((1+Random(65465)),16);
 if length(var1)>0 then Control1:= var1 else control1:= '0';
 showmessage(control1);
 num:= floor(2765476*Random);
 var1 := BaseChange(num,36);
 if length(var1)>0 then control1 := var1 else control1:= '0';
 showMessage(Control1);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now