TAL Programmer's Guide

Binary-to-ASCII Conversion Program
Sample Programs
A–6 096254 Tandem Computers Incorporated
Example A-5. C- or D-Series Binary-to-ASCII Conversion Program
!1! !INT PROC ASCII converts a binary INT value to an ASCII
! (base 10) value of up to six characters (including the
! sign) and returns the ASCII value and its length.
INT PROC ascii (v, rjust, stg);
!2! INT v; !INT value to convert
INT rjust; !Right justify result flag
STRING .stg; !Target string
BEGIN
!3! STRING .b[0:5] := [5*[" "],"0"];
INT n; !String length
INT sgn := 0; !Nonzero if V is negative
INT k := 5; !Index for converted digit
!4! IF v < 0 THEN !Value is negative
BEGIN
sgn := 1; !Set negative value flag
v := -v; !Take absolute value
END;
!5! WHILE v DO !While a value is left . . .
BEGIN ! (equivalent to V <> 0)
!6! b[k] := $UDBL(v) '\' 10 + "0";
!Convert a character
v := v / 10; !Compute remainder
k := k - 1; !Count converted character
END;
IF sgn THEN !Number is negative
BEGIN
b[k] := "-"; !Insert the sign
k := k - 1; !Count it as a character
END;
!7! IF NOT (n:=5-k) THEN !Check for an underflow
n := 1; !Return 1 character in that case
!8! IF rjust THEN !Move string to target
stg[n-1] '=:' b[5] FOR n BYTES
!Reverse move if right justified
ELSE
stg ':=' b[6-n] FOR n BYTES;
!Otherwise forward move
!9! RETURN n; !Return string’s length
END !ascii! ;