Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//
//INCLUDE files for :Convert long to a s
// tring
//**************************************
//
/* +++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 */
/*
LTOA.C -- routine and example program to convert a long int to
the specified numeric base, from 2 to 36.
Written by Thad Smith III, Boulder, CO. USA 9/06/91
and contributed to the public Domain.
Parameters: 1 - number to be converted
2 - buffer in which to build the converted string
3 - number base to use for conversion (2-36)
Returns: A character pointer to the converted string if
successful, a NULL pointer if the number base specified
is out of range.
*/
#include
#include "numcnvrt.h"
#if defined(__ZTC__) && !defined(__SC__)
char *ltoa(
long val, /* value to be converted */
char *buf,/* output string */
int base) /* conversion base*/
{
ldiv_t r; /* result of val / base */
if (base > 36 || base < 2) /* no conversion if wrong base */
{
*buf = '\0';
return buf;
}
if (val < 0)
*buf++ = '-';
r = ldiv (labs(val), base);
/* output digits of val/base first */
if (r.quot > 0)
buf = ltoa ( r.quot, buf, base);
/* output last digit */
*buf++ = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem];
*buf= '\0';
return buf;
}
#endif
#ifdef TEST
#include
main()
{
char buf[100], line[100], *tail;
long v;
int inbase, outbase;
for (;;)
{
printf ("inbase, value, outbase (or hit Ctrl-C to exit)? ");
fgets (line, sizeof line, stdin);
sscanf (line, " %d%*[, ]%[^, ]%*[, ]%d", &inbase, buf, &outbase);
if (inbase == 0)
break;/* exit if first number 0 */
v = strtol (buf, &tail, inbase);
printf("%s(%d) = %ld(10)\n", buf, inbase, v);
ltoa ( v, buf, outbase);
printf("ltoa(%ld, buf, %d) = %s\n\n", v, outbase, buf);
printf ("=%ld (10) = %s (%d).\n", v, buf, outbase);
};
return 0;
}
#endif /* TEST */