[9] | 1 | (*
|
---|
| 2 | (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
---|
| 3 | See the copyright notice in the ACK home directory, in the file "Copyright".
|
---|
| 4 | *)
|
---|
| 5 |
|
---|
| 6 | (*$R-*)
|
---|
| 7 | IMPLEMENTATION MODULE Conversions;
|
---|
| 8 | (*
|
---|
| 9 | Module: numeric-to-string conversions
|
---|
| 10 | Author: Ceriel J.H. Jacobs
|
---|
| 11 | Version: $Header: /cvsup/minix/src/lib/ack/libm2/Conversion.mod,v 1.1 2005/10/10 15:27:46 beng Exp $
|
---|
| 12 | *)
|
---|
| 13 |
|
---|
| 14 | PROCEDURE ConvertNum(num, len, base: CARDINAL;
|
---|
| 15 | neg: BOOLEAN;
|
---|
| 16 | VAR str: ARRAY OF CHAR);
|
---|
| 17 | VAR i: CARDINAL;
|
---|
| 18 | r: CARDINAL;
|
---|
| 19 | tmp: ARRAY [0..20] OF CHAR;
|
---|
| 20 | BEGIN
|
---|
| 21 | i := 0;
|
---|
| 22 | REPEAT
|
---|
| 23 | r := num MOD base;
|
---|
| 24 | num := num DIV base;
|
---|
| 25 | IF r <= 9 THEN
|
---|
| 26 | tmp[i] := CHR(r + ORD('0'));
|
---|
| 27 | ELSE
|
---|
| 28 | tmp[i] := CHR(r - 10 + ORD('A'));
|
---|
| 29 | END;
|
---|
| 30 | INC(i);
|
---|
| 31 | UNTIL num = 0;
|
---|
| 32 | IF neg THEN
|
---|
| 33 | tmp[i] := '-';
|
---|
| 34 | INC(i)
|
---|
| 35 | END;
|
---|
| 36 | IF len > HIGH(str) + 1 THEN len := HIGH(str) + 1; END;
|
---|
| 37 | IF i > HIGH(str) + 1 THEN i := HIGH(str) + 1; END;
|
---|
| 38 | r := 0;
|
---|
| 39 | WHILE len > i DO str[r] := ' '; INC(r); DEC(len); END;
|
---|
| 40 | WHILE i > 0 DO str[r] := tmp[i-1]; DEC(i); INC(r); END;
|
---|
| 41 | WHILE r <= HIGH(str) DO
|
---|
| 42 | str[r] := 0C;
|
---|
| 43 | INC(r);
|
---|
| 44 | END;
|
---|
| 45 | END ConvertNum;
|
---|
| 46 |
|
---|
| 47 | PROCEDURE ConvertOctal(num, len: CARDINAL; VAR str: ARRAY OF CHAR);
|
---|
| 48 | BEGIN
|
---|
| 49 | ConvertNum(num, len, 8, FALSE, str);
|
---|
| 50 | END ConvertOctal;
|
---|
| 51 |
|
---|
| 52 | PROCEDURE ConvertHex(num, len: CARDINAL; VAR str: ARRAY OF CHAR);
|
---|
| 53 | BEGIN
|
---|
| 54 | ConvertNum(num, len, 16, FALSE, str);
|
---|
| 55 | END ConvertHex;
|
---|
| 56 |
|
---|
| 57 | PROCEDURE ConvertCardinal(num, len: CARDINAL; VAR str: ARRAY OF CHAR);
|
---|
| 58 | BEGIN
|
---|
| 59 | ConvertNum(num, len, 10, FALSE, str);
|
---|
| 60 | END ConvertCardinal;
|
---|
| 61 |
|
---|
| 62 | PROCEDURE ConvertInteger(num: INTEGER;
|
---|
| 63 | len: CARDINAL;
|
---|
| 64 | VAR str: ARRAY OF CHAR);
|
---|
| 65 | BEGIN
|
---|
| 66 | IF (num < 0) AND (num >= -MAX(INTEGER)) THEN
|
---|
| 67 | ConvertNum(-num, len, 10, TRUE, str);
|
---|
| 68 | ELSE
|
---|
| 69 | ConvertNum(CARDINAL(num), len, 10, num < 0, str);
|
---|
| 70 | END;
|
---|
| 71 | END ConvertInteger;
|
---|
| 72 |
|
---|
| 73 | END Conversions.
|
---|