Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//
//INCLUDE files for :A safer version of
// ltoa()
//**************************************
//
/* +++Date last modified: 05-Jul-1997 */
/*
** NUMCNVRT.H - Header file for SNIPPETS numerical <=> string conversions
*/
#ifndef NUMCNVRT__H
#define NUMCNVRT__H
#include
#include "sniptype.h"
#include "round.h"
#include "pi.h"
#define R_ERROR -2 /* EVAL.C Range error*/
/*
** Callable library functions begin here
*/
char * base_convert(const char *in, char *out,
size_t len, int rin, int rout);/* Bascnvrt.C */
char * comma_float(double num, char *buf, int dec); /* Commaflt.C */
size_t commafmt(char *buf, int bufsize, long N); /* Commafmt.C */
char * eng(double value, int places);/* Eng.C */
int evaluate(char *line, double *val);/* Eval.C */
char * fmt_money(double amt);/* Fmtmoney.C */
longhexorint(const char *string); /* Hexorint.C */
char * ltostr(long num, char *string,
size_t max_chars, unsigned base); /* Ltostr.C*/
char * ordinal_text(int number); /* Ord_Text.C */
int scanfrac (const char buf[], double *f);/* Scanfrac.C */
unsigned int hstr_i(char *cptr); /* Hstr_I.C*/
char *long2roman(long val, char *buf, size_t buflen); /* L2Roman.C */
long roman2long(const char *str);/* Roman2L.C */
#if defined(__ZTC__) && !defined(__SC__)
char * ltoa(long val, char *buf, int base); /* Ltoa.C */
#endif
/*
** File: STR27SEG.C
*/
struct Seg7disp {
unsigned seg_a : 1;
unsigned seg_b : 1;
unsigned seg_c : 1;
unsigned seg_d : 1;
unsigned seg_e : 1;
unsigned seg_f : 1;
unsigned seg_g : 1;
};
char *str27seg(char *string);
#endif /* NUMCNVRT__H */
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//**************************************
//
/* +++Date last modified: 05-Jul-1997 */
/*
** LTOSTR.C - An improved, safer, ltoa()
On call:
num=number to convert
string=buffer for output
max_chars =maximum size of buffer
base =number base for conversion.
return value:
if illegal base
NULL
beginning of converted number.
notes: if number is too large in magnitude to fit in the buffer,
the MOST significant digits will be truncated. If the number is
negative, a leading '-' will be placed in the buffer even if this
causes the most significant digit to be truncated.
The number is right justified in the buffer and the location of the
first character in the number is returned so:
if you want right justification, use the original string.
if you want left justification, use the returned string.
if the number doesn't fill the buffer:
leading characters will be filled with spaces.
public domain by Jerry Coffin
*/
#include
#include
#include "numcnvrt.h"
char *ltostr(long num, char *string, size_t max_chars, unsigned base)
{
char remainder;
int sign = 0;/* number of digits occupied by the sign. */
if (base < 2 || base > 36)
return NULL;
if (num < 0)
{
sign = 1;
num = -num;
}
string[--max_chars] = '\0';
for (max_chars--; max_chars > sign && num!=0; max_chars --)
{
remainder = (char) (num % base);
if ( remainder < 9 )
string[max_chars] = remainder + '0';
else string[max_chars] = remainder - 10 + 'A';
num /= base;
}
if (sign)
string[--max_chars] = '-';
if ( max_chars > 0 )
memset(string, ' ', max_chars+1);
return string + max_chars;
}
#ifdef TEST
#include
#ifdef __WATCOMC__
#pragma off (unreferenced);
#endif
#ifdef __TURBOC__
#pragma argsused
#endif
#define SIZE 50
int main(int argc, char *argv[])
{
char buffer[SIZE];
long number= atoi(argv[1]);
unsigned base = atoi(argv[2]);
printf("%ld in base %u is \"%s\"\n", number, base,
ltostr(number, buffer, SIZE, base));
return 0;
}
#endif /* TEST */