Message-ID: <35C00E04.B44CA66D@mailexcite.com> Date: Thu, 30 Jul 1998 02:09:10 -0400 From: Doug Gale MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: sprintf and 64 bit integers References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: oshawappp84.idirect.com Organization: "Usenet User" Lines: 123 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Andrew Deren wrote: > You can just use this little function I came up with now instead of > sprintf: > void LongLongToChar(char* string, long long number) > { > long long div = number; > long mod; > int i = 0; > int len; > char tempChar; > > do { > mod = div % 10; > div = div / 10; > string[i++] = mod + '0'; /* that is zero */ > } while (div > 0); > > string[i] = '\0'; > len = i - 1; > > /* now just reverse the string */ > for (i=0; i<(len+1)/2; i++) { > tempChar = string[i]; > string[i] = string[len-i]; > string[len-i] = tempChar; > } > } > > If you also need to support negative numbers, just first test whether the > number is negative it it is make it possitive and insert '-' at the > beginning of the string. > > On Tue, 28 Jul 1998, Niki Ruusunko wrote: > > > I have this problem with a function I use to do thousands separators to > > integers. Here is the code: > > > > char * LargeInt(unsigned int bignumber) > > { > > int counter = -1, i, i2 = 0, sep = 0; > > char string1[21]; > > char string2[27]; > > sprintf(string1, "%d", bignumber); > > i = strlen(string1); > > while(i >= 0) > > { > > if (counter < 3) > > { > > string2[i2] = string1[i]; > > counter++; > > } > > else > > { > > string2[i2] = ','; > > i2++; > > string2[i2] = string1[i]; > > counter = 1; > > sep++; > > } > > i--; > > i2++; > > } > > i = 0; > > i2 = strlen(string1) + sep; > > while(i2 >= 0) > > { > > tmpInt[i] = string2[i2]; > > i++; > > i2--; > > } > > return tmpInt; > > } > > > > It isn't very good function but it works (on ints that is). The problem is > > that I need it to support the "long long" -variables I use. Sprintf doesn't > > compile with a long long as the argument (it says: "warning: int format, > > different type arg (arg 3)") so I'm asking if there is an alternate > > function to make a string out of an int (or, a long long in this case). > > > > Of course, if you know how to make the separators some better way, please > > tell me. > > Well, I have a better way to add commas to while converting to string. I could go dig it out (I have it _somewhere_, but this should be close enough to working code to get you started). It does have one requirement: you must put a null terminator at the start of the buffer before calling it. void NumToStrWithCommas(char *buf, unsigned num) { char buf2[10]; if (num > 999) NumToStrWithCommas(buf, num / 1000); if (num > 999) sprintf(buf2, ",%03u", num % 1000)' else sprintf(buf2, "%3u", num); strcat(buf, buf2); } You see how it works? It keeps calling itself until it is into the most significant triple of numbers. Then, as it returns back, it appends the triples of digits to "buf". This is called "recursion" and is the first time I have actually made a good use of it. As for the original topic of this thread, the int paramter can be substituted for "long long". Either ignore the printf warnings, or cast the parameter in the printf calls to (unsigned). NOTE: this only works for positive numbers, but you could easily add a wrapper that detects negative, puts '-' in buf, and calls this with an absolute number. Whew. I think I've covered everything! :)