add ultostr utility routine, inverse of strtoul

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@1320 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 1998-10-18 21:13:40 +00:00
parent cf0fd7af1f
commit 047f4c1213
2 changed files with 55 additions and 0 deletions

View File

@ -2,6 +2,7 @@
* util.c -- utility functions that are used everywhere else for *
* xacc (X-Accountant) *
* Copyright (C) 1997 Robin D. Clark *
* Copyright (C) 1997, 1998 Linas Vepstas *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
@ -79,6 +80,52 @@ safe_strcmp (const char * da, const char * db) {
return 0;
}
/********************************************************************\
\********************************************************************/
/* inverse of strtoul */
#define MAX_DIGITS 50
char *
ultostr (unsigned long val, int base)
{
char buf[MAX_DIGITS];
unsigned long broke[MAX_DIGITS];
int i;
unsigned long places=0, reval;
if ((2>base) || (36<base)) return NULL;
/* count digits */
places = 0;
for (i=0; i<MAX_DIGITS; i++) {
broke[i] = val;
places ++;
val /= base;
if (0 == val) break;
}
/* normalize */
reval = 0;
for (i=places-2; i>=0; i--) {
reval += broke[i+1];
reval *= base;
broke[i] -= reval;
}
/* print */
for (i=0; i<places; i++) {
if (10>broke[i]) {
buf[places-1-i] = 0x30+broke[i]; /* ascii digit zero */
} else {
buf[places-1-i] = 0x41-10+broke[i]; /* ascii capaital A */
}
}
buf[places] = 0x0;
return strdup (buf);
}
/********************************************************************\
* currency & locale related stuff.
* first attempt at internationalization i18n of currency amounts

View File

@ -108,6 +108,14 @@ size_t dcoresize();
int safe_strcmp (const char * da, const char * db);
/********************************************************/
/* the ultostr() subroutihne is the inverse of strtoul().
* It accepts a number and prints it in the indicated base.
* The returned string should be freed when done.
*/
char * ultostr (unsigned long val, int base);
/** PROTOTYPES ******************************************************/
#define PRTSYM 0x1