[comp.lang.pascal] Integer to String Conversion

chaudhury@physics.ucla.edu (CHAUDHURY, SHILADITYA) (09/26/90)

Hi,
  I wonder if someone could point out a simple way to do integer to string
conversion in SUN Pascal. I generate a number that needs to be part of a 
filename in :
		rewrite(textfile,filename); 
where 'filename' is a string. Right now I can write the integer to a text
file and read it back as a string - but that's rather dumb!
I have been too used to nice Turbo Pascal features for such things...

--Raj Chaudhury
UCLA, Solid State Science Center

ts@uwasa.fi (Timo Salmi) (09/26/90)

In article <24602@adm.BRL.MIL> chaudhury@physics.ucla.edu (CHAUDHURY, SHILADITYA) writes:
>  I wonder if someone could point out a simple way to do integer to string
>conversion in SUN Pascal. I generate a number that needs to be part of a 
>filename in :

There is no trivial solution in a standard Pascal although the
resulting code will be brief.  Here is the outline.  To convert an
integer to a string you'll need a loop, which extracts the digits
from the integer.  This is achieved by using mod.  Then you convert
a digit by using chr plus ascii for 0.  To control the loop and to
extract successive digits you must div the integer until the
division produces 0.  In order for the conversion to be general, you
must also test for the sign.  It is easier to convert a non-negative
integer, and then augment the sign to the beginning of the string. 

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

reagan@hiyall.enet.dec.com (John R. Reagan) (09/28/90)

In article <1990Sep26.082929.9182@uwasa.fi>, ts@uwasa.fi (Timo Salmi) writes...
>In article <24602@adm.BRL.MIL> chaudhury@physics.ucla.edu (CHAUDHURY, SHILADITYA) writes:
>>  I wonder if someone could point out a simple way to do integer to string
>>conversion in SUN Pascal. I generate a number that needs to be part of a 
>>filename in :
> 
>There is no trivial solution in a standard Pascal although the
>resulting code will be brief.  ...

The new Pascal standard (Extended Pascal) now has a standard method
to do this.  You use the WRITESTR builtin routine.  For example,

var i : integer;
    s : string(32);

  begin
  i := 35;
  writestr(s,i);
  writeln(s);
  end

---
John Reagan
Digital Equipment Corporation
reagan@hiyall.enet.dec.com
Disclaimer:  The opinions and statements expressed by me are not
             necessarily those of Digital Equipment Corporation.
---