Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!think.com!sdd.hp.com!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!nj From: nj@magnolia.Berkeley.EDU (Narciso Jaramillo) Newsgroups: comp.sys.amiga.programmer Subject: Re: help Message-ID: Date: 17 May 91 17:42:25 GMT References: <1991May17.073147.737@crash.cts.com> Sender: nobody@ucbvax.BERKELEY.EDU Organization: Postcarcinogenic Bliss, Inc. Lines: 35 In-reply-to: neil@pnet01.cts.com's message of 17 May 91 07:31:47 GMT In article <1991May17.073147.737@crash.cts.com> neil@pnet01.cts.com (Neil Coito) writes: >I'm using Lattice 5.10 and need help converting a ULONG variable to a string. >[...] In the Lattice manual there are conversions >for EVERYTHING ON THIS DAMN PLANET BUT NOTHING TO CONVERT AN UNLONG OR INT TO >A STRING. Please help!!! Take a deep breath; help is on the way. You may have noticed that there are plenty of functions for converting strings to other things, but no functions for converting other things to strings. That's because there's a general function for doing that, called ``sprintf.'' It formats its arguments just like printf, but instead of outputting them to the console, it writes the result into a string. So, in order to convert a ULONG into a string: char str[20]; /* actually, ULONGs can't be even this long */ ULONG longo; longo = (1 << 31) /* my goodness, that's long! */ sprintf(str, "%lu", longo); The string str now contains the value of longo, but in ASCII format. It's just as general as printf--anything printf can format, sprintf can format--so you can use it to convert any type of variable to a string. One thing to be careful of (not just here, but generally): Be sure that the char * you pass to it actually points to allocated memory. In this case, I declared str as an array, so it's automatically allocated. If you declare it as a char *, though, you'll have to use malloc() or AllocMem() to request memory that sprintf can use to hold its result. nj