"Steve" <

XXXX@XXXXX.COM>schrieb im Newsbeitrag

##### Quote

Hi,

Hex type is something like $FF, what about Binary type?

Thanks

Steve

I'm afraid you need to code your binary constant as string

and wherever you want to use it, you need to convert the string

by a self made function.

In this manner I created a strtol() function similar to the C library.

program Bases;

{$ifdef Win32}

{$AppType Console}

const MaxLongestInt = $7FFFFFFFFFFFFFFF;

type LongestInt = int64;

{$else}

uses WinCRT;

const MaxLongestInt = $7FFFFFFF;

type LongestInt = LongInt;

{$endif}

var Number : LongestInt;

ErrorPos,

Base : Integer;

s : String;

function strtol(s : String; var ErrorPos : Integer; Base : Integer) :

LongestInt;

var l : LongestInt;

c : char;

begin

l := 0;

ErrorPos := 0;

c := #0;

while s>'' do

begin

inc(ErrorPos);

c := upcase(s[1]);

s := Copy(s, 2, length(s));

if ('0' <= c) and (c <= '9') and (c < chr(Base + ord('0'))) then

begin

l := l * Base + ord(c) - ord('0');

c := #0;

end

else

if ('A' <= c) and (c < chr(Base - 10 + ord('A'))) then

begin

l := l * Base + ord(c) - ord('A') + 10;

c := #0;

end

else

s := '';

end;

if c = #0 then

begin

strtol := l;

ErrorPos := 0;

end

else

strtol := 0;

end;

function ltoa(Number : LongestInt; Base : Integer) : String;

var s : String;

Digit : Integer;

begin

s := '';

if Number < 0 then

begin

{

positive Number = $100... + Number;

$100... = $80... + $40... + $40...;

Number and $7F... = Number + $80... avoiding overflow;

$40... = MaxLongestInt shr 1 + 1;

}

Number := Number and MaxLongestInt;

Digit := Number mod Base;

Number := Number div Base;

Number := Number + (MaxLongestInt shr 1 + 1 + Digit) div Base;

Digit := (MaxLongestInt shr 1 + 1 + Digit) mod Base;

Number := Number + (MaxLongestInt shr 1 + 1 + Digit) div Base;

Digit := (MaxLongestInt shr 1 + 1 + Digit) mod Base;

if Digit < 10 then

s := chr(Digit + ord('0')) + s

else

s := chr(Digit - 10 + ord('a')) + s;

end;

while Number <>0 do

begin

Digit := Number mod Base;

Number := Number div Base;

if Digit < 10 then

s := chr(Digit + ord('0')) + s

else

s := chr(Digit - 10 + ord('a')) + s;

end;

ltoa := s;

end;

begin

repeat

write('Number? ');

Readln(s);

write('Base? ');

Readln(Base);

if Base>= 2 then

begin

writeln(s, ' Base ', Base, ' is ', strtol(s, ErrorPos, Base), ' Base

10');

writeln('ErrorPos is ', ErrorPos);

writeln;

end;

until Base < 2;

writeln;

repeat

write('Number Base 10? ');

Readln(Number);

write('Base? ');

readln(Base);

if Base>= 2 then

begin

writeln(Number, ' Base 10 is ', ltoa(Number, Base), ' Base ', Base);

writeln;

end;

until Base < 2;

end.