Solved

php function to delphi

Posted on 2012-03-23
3
355 Views
Last Modified: 2012-03-24
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
Comment
Question by:alpires
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
gskoczylas earned 500 total points
ID: 37760364
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
 
LVL 9

Expert Comment

by:rinfo
ID: 37760452
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
 
LVL 9

Expert Comment

by:rinfo
ID: 37760514
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Echo'd values in dropdowns 6 53
How do I add counter to html listbox using jquery 2 41
"Could not obtain OLE control window handle" under Delphi XE8 8 74
Boolean 13 46
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question