home *** CD-ROM | disk | FTP | other *** search
- Unit BaseCon;
-
- {NORTHWESTERN UNIVERSITY TURBO USERS GROUP UTILITIES}
-
- (** NUtility BASECON ROUTINES **)
-
- {(C) J. E Hilliard 1986}
-
-
- {This is a set of functions for performing the following con-
- versions: Dec > Hex; Dec > Bin and BCD > Dec. }
-
- Interface
-
- FUNCTION OutHex (Dec : integer) : String;
- {Returns the hexadecimal value of a decimal integer or byte
- as a string with the prefix '$'. }
-
- FUNCTION OutBin (Dec : integer) : String;
- {Returns the binary value of 'Dec' as a string. }
-
- FUNCTION InBCD (Input : integer) : integer;
-
- {This function returns the value of a Binary Coded Decimal
- integer or byte. The BCD format is frequently used by Clock/
- Calendar boards. }
-
- {/Numbers are normally stored in binary format. An alterna-
- tive format is Binary Coded Decimal (BCD). Four bits are
- assigned to each decimal digit (nibbles $0 to $9). The six
- nibbles $A to $F are not used. This is obviously an unecon-
- omical use of storage. But BCD's have advantages, which
- include easy conversion to ASCII code and a well defined
- precision. /}
-
- Implementation
-
- TYPE
-
- String20 = string[20];
- String10 = string[10];
-
- FUNCTION OutHex (Dec : integer) : String;
-
- {Returns the hexadecimal value of a decimal integer or byte
- as a string with the prefix '$'. }
-
- VAR
-
- Int1 : integer;
- HoldS : String10;
-
- Begin
-
- HoldS := '';
-
- repeat
-
- Int1 := Dec and $F; {Mask off low byte. }
- if Int1 < 10
- then {Numeric reqd. }
- HoldS := chr (48 + Int1) + HoldS {#48 = '0'. }
- else {Character reqd. }
- HoldS := chr (55 + Int1) + HoldS; {#65 = 'A'. }
- Dec := Dec shr 4; {Get next byte if any. }
-
- until Dec = 0;
-
- if length (HoldS) mod 2 <> 0
- then {Append leading 0 if nec. }
- HoldS := '0' + HoldS;
-
- OutHex := '$' + HoldS; {Append 'H' if preferred. }
-
- End; {OutHex (Dec : integer) : String10}
-
-
- FUNCTION OutBin (Dec : integer) : String;
-
- {Returns the binary value of 'Dec' as a string. }
-
- VAR
-
- J : integer;
- Int1 : integer;
- HoldS : string[20];
-
- Begin
-
- HoldS := '';
- Int1 := Dec;
- repeat
-
- for J := 1 to 8 do
- begin
- if odd (Int1) {Interesting application of 'odd'. }
- then
- HoldS := '1' + HoldS
- else
- HoldS := '0' + HoldS;
- Int1 := Int1 shr 1; {Get next bit. }
- end; {for J}
- if (Dec > 127) or (Dec < 0)
- then
- begin
- HoldS := ' ' + HoldS; {Add separator between the two sets }
- Dec := 0; {of 8 bits. }
- end; {if}
-
- until Int1 = 0;
-
- OutBin := HoldS;
-
- End; {OutBin (Dec : byte) : String20}
-
-
- FUNCTION InBCD (Input : integer) : integer;
-
- {This function returns the value of a Binary Coded Decimal
- integer or byte. The BCD format is frequently used by Clock/
- Calendar boards. }
-
- {/Numbers are normally stored in binary format. An alterna-
- tive format is Binary Coded Decimal (BCD). Four bits are
- assigned to each decimal digit (nibbles $0 to $9). The six
- nibbles $A to $F are not used. This is obviously an unecon-
- omical use of storage. But BCD's have advantages, which
- include easy conversion to ASCII code and a well defined
- precision. /}
-
-
- VAR
-
- Int1 : integer;
-
- Begin
-
- Int1 := Lo (Input) and $F + 10 * ( Lo (Input) shr 4);
- if Hi (Input) > 0 {Don't waste time if Input is a byte. }
- then
- Int1 := Int1 + 100 * ( Hi (Input) and $F) +
- 1000 * ( Hi (Input) shr 4);
-
- InBCD := Int1;
-
- End; {InBCD (Input : integer) : integer}
-
- Begin (* Of Initialization *)
- End.
-