1. int2str.e

I needed to convert an integer to a string for using it in a file
name for instance. I overlooked it someplace imagine but I couldn't find any
built in function to do so. I wrote the following which handles the
problem...but tell me, is there a built in way? Thanks, Art Adamson

--convert an int to a string
global function intToStr(integer tempInt)
sequence seqOut
seqOut = {}
while   floor(tempInt) do
        seqOut = prepend(seqOut,  48 + remainder(tempInt,10))
        tempInt = floor(tempInt / 10)
end while
return seqOut
end function    --intToStr

--puts(1,"The sequence returned is:  ")
--? intToStr(1234)
--puts(1,intToStr(1234))
Arthur P. Adamson, The Cincinnati Engine Man, euclid at isoc.net

new topic     » topic index » view message » categorize

2. Re: int2str.e

Arthur Adamson wrote:
>         I needed to convert an integer to a string for using it in a file
> name for instance. I overlooked it someplace imagine but I couldn't find any
> built in function to do so. I wrote the following which handles the
> problem...but tell me, is there a built in way? Thanks, Art Adamson
>
> --convert an int to a string
> global function intToStr(integer tempInt)
> sequence seqOut
> seqOut = {}
> while   floor(tempInt) do
>         seqOut = prepend(seqOut,  48 + remainder(tempInt,10))
>         tempInt = floor(tempInt / 10)
> end while
> return seqOut
> end function    --intToStr
>
> --puts(1,"The sequence returned is:  ")
> --? intToStr(1234)
> --puts(1,intToStr(1234))
> Arthur P. Adamson, The Cincinnati Engine Man, euclid at isoc.net

Did you try sprintf?

--puts(1,"The sequence returned is:  ")
--? sprintf("%d",{1234})
--puts(1,sprintf("%d",{1234}))

It does hex and floating point too.
--
                   _____         _____        _____
    ________      /\    \       /\    \      /\    \
   /   \    \    /  \____\     /  \____\    /  \____\
  /  _  \____\  /   / ___/_   /   /____/   /   / ___/_
 /  / \  [___] /   / /\____\ /    \    \  /   / /\____\
 \  \_/ /    / \   \/ / ___/_\     \    \ \   \/ / ___/_
  \    /____/   \    / /\    \\/\   \    \ \    / /\    \
   \   \    \    \   \/  \____\  \   \    \ \   \/  \____\
    \   \    \    \      /    /   \   \____\ \      /    /
     \   \____\    \    /    /     \  /    /  \    /    /
      \  /    /     \  /    /       \/____/    \  /    /
       \/____/       \/____/xseal at harborside.com\/____/

new topic     » goto parent     » topic index » view message » categorize

3. int2str.e

Aruthur Adamson wrote:
>   I needed to convert an integer to a string for using it in a file
>name for instance. I overlooked it someplace imagine but I couldn't find=

any
>built in function to do so. I wrote the following which handles the
>problem...but tell me, is there a built in way? Thanks, Art Adamson
>
>--convert an int to a string
>global function intToStr(integer tempInt)
>sequence seqOut
>seqOut =3D {}
>while   floor(tempInt) do
>        seqOut =3D prepend(seqOut,  48 + remainder(tempInt,10))
>        tempInt =3D floor(tempInt / 10)
>end while
>return seqOut
>end function    --intToStr

>--puts(1,"The sequence returned is:  ")
>--? intToStr(1234)
>--puts(1,intToStr(1234))
>Arthur P. Adamson, The Cincinnati Engine Man, euclid at isoc.net

Actually Arthur, there is a function call "sprintf" that will accomplish
this for you.  The syntax is:
s =3D sprintf(st,x)
where st is a format string, and x is the number.  It is used like this:
integer num
sequence s
num =3D 15
s =3D sprintf("%03d",num)
-- s comes out to be "015"

I hope this helps.   If you need further assistance, check the library
routines
in the included documentation.

Regards,
  Bryan Watts

new topic     » goto parent     » topic index » view message » categorize

4. Re: int2str.e

Daniel Berstein wrote:
>
> On 25 Oct 97 , Pete Eberlein wrote:
>
> > Did you try sprintf?
> >
> > --puts(1,"The sequence returned is:  ")
> > --? sprintf("%d",{1234})
> > --puts(1,sprintf("%d",{1234}))
> >
> > It does hex and floating point too.
>
> But how do you store that string in a variable (sequence) for further
> use?

Um, I haven't tested this, but you might be able to use the assignment
operator:

sequence s
s = sprintf("%d",{1234})
puts(1,"The sequence returned is:  ")
? s
puts(1, s)

The sprintf function returns a sequence formatted the same way as the
printf procedure.  The following two lines are equivalent:

printf(1, "%d\n", {1234})
puts(1, sprintf("%d\n", {1234}))

--
                   _____         _____        _____
    ________      /\    \       /\    \      /\    \
   /   \    \    /  \____\     /  \____\    /  \____\
  /  _  \____\  /   / ___/_   /   /____/   /   / ___/_
 /  / \  [___] /   / /\____\ /    \    \  /   / /\____\
 \  \_/ /    / \   \/ / ___/_\     \    \ \   \/ / ___/_
  \    /____/   \    / /\    \\/\   \    \ \    / /\    \
   \   \    \    \   \/  \____\  \   \    \ \   \/  \____\
    \   \    \    \      /    /   \   \____\ \      /    /
     \   \____\    \    /    /     \  /    /  \    /    /
      \  /    /     \  /    /       \/____/    \  /    /
       \/____/       \/____/xseal at harborside.com\/____/

new topic     » goto parent     » topic index » view message » categorize

5. Re: int2str.e

Thanks to Pete Eberlein and others for solving my problem. Sorry I
overlooked the solution. Bye, Art
----------------------------------------------------

>Arthur Adamson wrote:
>>         I needed to convert an integer to a string for using it in a file
Arthur P. Adamson, The Cincinnati Engine Man, euclid at isoc.net

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu