1987-08-19 18:07:01 +00:00
|
|
|
(*$R-*)
|
1987-05-13 14:36:45 +00:00
|
|
|
IMPLEMENTATION MODULE Conversions;
|
|
|
|
|
|
|
|
PROCEDURE ConvertNum(num, len, base: CARDINAL;
|
|
|
|
neg: BOOLEAN;
|
|
|
|
VAR str: ARRAY OF CHAR);
|
|
|
|
VAR i: CARDINAL;
|
|
|
|
r: CARDINAL;
|
|
|
|
tmp: ARRAY [0..20] OF CHAR;
|
|
|
|
BEGIN
|
|
|
|
i := 0;
|
|
|
|
REPEAT
|
|
|
|
r := num MOD base;
|
|
|
|
num := num DIV base;
|
|
|
|
IF r <= 9 THEN
|
|
|
|
tmp[i] := CHR(r + ORD('0'));
|
|
|
|
ELSE
|
|
|
|
tmp[i] := CHR(r - 10 + ORD('A'));
|
|
|
|
END;
|
|
|
|
INC(i);
|
|
|
|
UNTIL num = 0;
|
1987-05-13 16:30:06 +00:00
|
|
|
IF neg THEN
|
|
|
|
tmp[i] := '-';
|
|
|
|
INC(i)
|
|
|
|
END;
|
1987-05-13 14:36:45 +00:00
|
|
|
IF len > HIGH(str) + 1 THEN len := HIGH(str) + 1; END;
|
|
|
|
IF i > HIGH(str) + 1 THEN i := HIGH(str) + 1; END;
|
|
|
|
r := 0;
|
|
|
|
WHILE len > i DO str[r] := ' '; INC(r); DEC(len); END;
|
|
|
|
WHILE i > 0 DO str[r] := tmp[i-1]; DEC(i); INC(r); END;
|
1988-01-28 16:37:55 +00:00
|
|
|
WHILE r <= HIGH(str) DO
|
|
|
|
str[r] := 0C;
|
|
|
|
INC(r);
|
|
|
|
END;
|
1987-05-13 14:36:45 +00:00
|
|
|
END ConvertNum;
|
|
|
|
|
|
|
|
PROCEDURE ConvertOctal(num, len: CARDINAL; VAR str: ARRAY OF CHAR);
|
|
|
|
BEGIN
|
|
|
|
ConvertNum(num, len, 8, FALSE, str);
|
|
|
|
END ConvertOctal;
|
|
|
|
|
|
|
|
PROCEDURE ConvertHex(num, len: CARDINAL; VAR str: ARRAY OF CHAR);
|
|
|
|
BEGIN
|
|
|
|
ConvertNum(num, len, 16, FALSE, str);
|
|
|
|
END ConvertHex;
|
|
|
|
|
|
|
|
PROCEDURE ConvertCardinal(num, len: CARDINAL; VAR str: ARRAY OF CHAR);
|
|
|
|
BEGIN
|
|
|
|
ConvertNum(num, len, 10, FALSE, str);
|
|
|
|
END ConvertCardinal;
|
|
|
|
|
|
|
|
PROCEDURE ConvertInteger(num: INTEGER;
|
|
|
|
len: CARDINAL;
|
|
|
|
VAR str: ARRAY OF CHAR);
|
|
|
|
BEGIN
|
1987-06-23 17:38:18 +00:00
|
|
|
IF (num < 0) AND (num >= -MAX(INTEGER)) THEN
|
1987-05-13 14:36:45 +00:00
|
|
|
ConvertNum(-num, len, 10, TRUE, str);
|
|
|
|
ELSE
|
1987-08-04 08:59:50 +00:00
|
|
|
ConvertNum(CARDINAL(num), len, 10, num < 0, str);
|
1987-05-13 14:36:45 +00:00
|
|
|
END;
|
|
|
|
END ConvertInteger;
|
|
|
|
|
|
|
|
END Conversions.
|