[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)


>     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
    if Odd(x) then
      s := '1' + s
      s := '0' + s;
    x := x div 2;

> 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):

unit Stuff;


function Twice(x: Integer): Integer;


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


And have the program:

{$apptype console}
uses Stuff;
  num: Integer;
  Write('Enter number: ');
  Writeln('Double = ', Twice(num));

> 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;
  // initialization code

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

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

More information about the fpc-devel mailing list