bmptp@amdcad.AMD.COM (Trina Phan-Vu) (05/05/88)
I am posting this for a friend, please respond your answer in this account then I will forward the results to her. Thank you for your time. My friend needs an algorithm of the conversion from integer to numeric string in Pascal, i.e. an integer"369" to a numeric "369". She knew how to do it in C, so please only help in Pascal.
jik@athena.mit.edu (Jonathan I. Kamens) (05/06/88)
Although I find that the question asked was a bit vague (How do you convert a "integer '369'" to a "numeric '369'?"), I assume that what is being asked for is a piece of standard pascal code which will convert an integer into a packed array of char. I've enclosed code below which will do that -- it's fairly straightforward, actually. If you are working on a Unix system, you can probably use the C libraries which do the same thing, if you hack things around enough. ************************* program convertertest(input, output); const MAXLEN = 12; { Maximum length of a numstring. This can probably be set to a reasonably low number on most systems, since the length of an integer on most systems is restricted to far less than the highest possible length of a string. } type numstring = packed array[0..MAXLEN] of char; var num : integer; final : numstring; procedure numtostring ( number : integer; { In } var result : numstring); { Out } { Purpose: takes as input an integer and converts it to a string Input: integer in number Output: converted string in result Diagnostics: If the integer is longer than will fit in the string, result[0] will be assigned chr(0). Otherwise, it is set to the length of the string. Author: Jonathan Kamens, MIT '91, jik@ATHENA.MIT.EDU } var factor : integer; digit : integer; place : integer; begin for place := 0 to MAXLEN do result[place] := chr(0); place := 0; factor := 1; while (number / factor) >= 10 do factor := factor * 10; while ((factor >= 1) and (place < MAXLEN)) do begin digit := number div factor; number := number - (digit * factor); place := place + 1; result[0] := chr(place); result[place] := chr(ord('0') + digit); factor := factor div 10 end; if (factor >= 1) then result[0] := chr(0) end; begin repeat readln(num); numtostring(num, final); if final[0] = chr(0) then writeln('Number is too long to convert.') else writeln(final) until (num = 0); end. ************************* | "This has been a test of the emergency broadcasting -=> Jonathan I. Kamens | system. If there had been a real emergency, the MIT '91 | radio to which you are listening would have been jik@ATHENA.MIT.EDU | dissolved by the heatwave following the impact of | the first warhead."
kirsch@braggvax.arpa (David Kirschbaum) (05/06/88)
The first two functions functions (in Turbo Pascal v3.0, but should work elsewhere) convert numbers to strings. The function RtoS trims the leading space(s) that Turbo puts on a string using the STR(r:8:2, NrStr) setup. The next two procedures work the other way, and include a flashy little error-trapping routine. David Kirschbaum Toad Hall kirsch@braggvax.ARPA *** CUT HERE *** TYPE Str8 = STRING[8]; Str12 = STRING[12]; FUNCTION ItoS(i : INTEGER) : Str8; {convert integer to string.} VAR NrS : Str8; BEGIN STR(i,NrS); {create a working NrS, no padding} ItoS := NrS; END; {of ItoS} FUNCTION RtoS(r : REAL) : Str12; {convert real to string. In this case, I was working with dollars, so that's why just the two decimal points.} VAR NrS : Str12; BEGIN IF r = 0.0 THEN RtoS := '0.00' ELSE BEGIN STR(r:8:2,NrS); {create a working NrS, no padding} WHILE (LENGTH(NrS) > 0) AND (NrS[1] = ' ') DO Delete(NrS,1,1); {trim leading spaces} RtoS := NrS; {return string} END; END; {of RtoS} PROCEDURE Say_BadNr(NrS : Str8; code : INTEGER); {used by StoR and StoI} BEGIN Writeln; Writeln('Incorrect entry.'); Writeln(NrS); {display the response} NrS := ' '; {get a blank line} NrS[code] := '^'; {point to bad char} Writeln(NrS); {display the pointer} END; {of Say_BadNr} PROCEDURE StoR(NrS : Str8; VAR r : REAL; VAR code : INTEGER); {Convert string to real, return global variable code} BEGIN VAL(NrS,r,code); {convert to real} IF code <> 0 THEN BEGIN {error} Say_BadNr(NrS, code); {display error msg} r := 0; {clear the variable} END; END; {of StoR} PROCEDURE StoI(NrS : Str8; VAR i, code : INTEGER); {Convert string to integer, return variables i and code} BEGIN VAL(NrS,i,code); {convert to integer} IF code <> 0 THEN BEGIN {error} Say_BadNr(NrS, code); {display error msg} i := 0; {clear the variable} END; END; {of StoI}
bmptp@amdcad.AMD.COM (Trina Phan-Vu) (05/10/88)
Thank you all who responsed to the question of integer conversion. My friend already got the answer and she was so impressed with all the helps she got. Thanks again.