[fpc-devel]Q about FP additional questions

Barry Kelly dynagen at eircom.net
Wed Sep 26 06:23:05 CEST 2001


On Tue, 25 Sep 2001 15:36:30 -0600, mike <mike at forrentusa.com> wrote:

> 1) Is there a simple way to do
>     Char 2 Ascii Code ('A' => #65)

  Ord('A')

>     Char 2 Hex Code (#255 => $FF)

  IntToHex(Ord('A'), 2)

(assuming 'uses SysUtils;' at the top).

>     Char 2 Binary Code (#255 => %11111111)

This isn't particularly useful (I've certainly never had a use for it) -
it isn't in the RTL. You can get the desired effect with a loop of
checking for Odd, then 'div 2'.

  x := Ord('A');
  s := '';
  while x > 0 do
  begin
    if Odd(x) then
      s := '1' + s
    else
      s := '0' + s;
    x := x div 2;
  end;


> 2) Is there an upper limit for how large a file the compiler can
> process?
>     I mean I could virtually write all my source code into one single
> text file,
>     and run it as is through the compiler.

That wouldn't be a good idea from a maintenance perspective. You should
break it up into units, and include these units on the search path. You
only have to include the main program filename on the compiler's command
line - the compiler should find all the units by their names, as long as
the appropriate files are on the search path.

For example, you might have the unit Stuff.pas (should be called
Stuff.pas for auto-finding):

---8<---
unit Stuff;

interface

function Twice(x: Integer): Integer;

implementation

function Twice(x: Integer): Integer;
begin
  Result := x * 2;
end;

end.
--->8---

And have the program:

---8<---
{$apptype console}
uses Stuff;
var
  num: Integer;
begin
  Write('Enter number: ');
  Readln(num);
  Writeln('Double = ', Twice(num));
end.
--->8---

> and perhaps most importantly,
> 
> 3) Is there a way to write DLL files with FreePascal,
>     or can I call the executables in some way to perform the same
> operations?

You use 'library' instead of 'program'. The above program could be
written as a DLL that exported the Twice function:

---8<--- // SomeStuff.pas
library SomeStuff;
uses Stuff;
exports Twice;
begin
  // initialization code
end.
--->8---

Keep in mind that this will be using FreePascal's calling convention,
which is most likely 'register' for Delphi compatibility (I didn't check
recently). If you want to call the function from another language, you
should use stdcall; change the 'Twice' declaration in the Stuff unit to
this:

function Twice(x: Integer): Integer; stdcall;

-- Barry

-- 
  If you're not part of the solution, you're part of the precipitate.
Team JEDI: http://www.delphi-jedi.org
NNQ - Quoting Style in Newsgroup Postings
  http://web.infoave.net/~dcalhoun/nnq/nquote.html




More information about the fpc-devel mailing list