home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
programs
/
disk
/
misc
/
dcmp
/
source
/
source.lha
/
xtoy.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-02
|
2KB
|
118 lines
/*
** $RCSfile: xtoy.c,v $
** $Release$
** $Revision: 1.3 $
** $Date: 92/11/11 18:09:53 $
** $Author: tf $
** $State: Exp $
**
** Support functions for data type conversion
**
** (c) Copyright 1990,91 Tobias Ferber. All Rights Reserved.
*/
#include "xtoy.h"
static char rcs_id[] = "$Id: xtoy.c,v 1.3 92/11/11 18:09:53 tf Exp $";
char *strupr(char *s)
{ char c;
while(c=*s)
{ if('a'<=c && c<='z')
*s=c-('a'-'A');
++s;
}
return(s);
}
char *strlower(char *s)
{ char c;
while(c=*s)
{ if('A'<=c && c<='Z')
*s=c+('a'-'A');
++s;
}
return(s);
}
/* decimal to long conversion */
long dectol(char *str)
{ long val=0;
char c;
while(isdigit(c=*str))
{ val = (((val<<2)+val)<<1) + c-'0';
str++;
}
return(val);
}
/* hex to long conversion */
long hextol(char *s)
{ long t=0;
char c;
while(ishexdigit(c=*s))
{ if('0'<=*s && *s<='9')
t= t*0x10 +(c-'0');
else if('A'<=c && c<='F')
t= t*0x10 +(c-'A'+0x0A);
else if('a'<=c && c<='f')
t= t*0x10 +(c-'a'+0x0A);
s++;
}
return(t);
}
char *binstr(long n, int numdigits)
{ static char s[33];
int d;
unsigned long mask= (1L<<(((numdigits>32)?(numdigits=32): /* this is C ! */
((numdigits<1)?(numdigits=1):numdigits))-1));
for (d=0; d<numdigits; d++)
s[d]= (n & (mask>>d))?'1':'0';
s[numdigits]='\0';
return(s);
}
/*
*
* FUNCTION
*
* itor convert integer to roman digits string
*
* SYNOPSIS
*
* eos = itor(arabic, roman)
* char *eos; * pointer to the end of string
* int arabic; * integer (must be > 0)
* char *roman; * destination string
*
* DESCRIPTION
*
* Well, try it!
*
* KNOWN BUGS
*
* The conversion from arabic to roman digits only works for values > 0.
*
*/
char *itor(int n, char *s)
{ while(n>=1000) { *s++='M'; n-=1000; }
if(n>=900) { *s++='C'; *s++='M'; n-=900; }
if(n>=500) { *s++='D'; n-=500; }
if(n>=400) { *s++='C'; *s++='D'; n-=400; }
while(n>=100) { *s++='C'; n-=100; }
if(n>=90) { *s++='X'; *s++='C'; n-=90; }
if(n>=50) { *s++='L'; n-=50; }
if(n>=40) { *s++='X'; *s++='L'; n-=40; }
while(n>=10) { *s++='X'; n-=10; }
if(n>=9) { *s++='I'; *s++='X'; n-=9; }
if(n>=5) { *s++='V'; n-=5; }
if(n>=4) { *s++='I'; *s++='V'; n-=4; }
while(n>=1) { *s++='I'; n--; }
*s='\000';
return(s);
}