Path: utzoo!mnetor!uunet!husc6!bloom-beacon!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Newsgroups: comp.lang.pascal Subject: Re: integer conversion Message-ID: <5136@bloom-beacon.MIT.EDU> Date: 5 May 88 22:47:25 GMT References: <21422@amdcad.AMD.COM> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: jik@athena.mit.edu (Jonathan I. Kamens) Organization: Massachusetts Institute of Technology Lines: 79 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."