More abstracted num to string functions

This commit is contained in:
Stefan `Sec` Zehl 2011-07-31 00:53:14 +02:00
parent e911376fe6
commit 06ac802ee5
2 changed files with 50 additions and 2 deletions

View File

@ -206,7 +206,12 @@ int applyConfig(void);
#define SYSTICKSPEED 10
// itoa.c
#define F_ZEROS (1<<0)
#define F_LONG (1<<1)
#define F_SPLUS (1<<2)
#define F_SSPACE (1<<3)
const char* IntToStrX(unsigned int num, unsigned int mxlen);
const char* IntToStr(int num, unsigned int mxlen, char flag);
// simpletime.c

View File

@ -1,8 +1,8 @@
#define LEN 32
#include "basic/basic.h"
#define LEN 10
const char* IntToStrX(unsigned int num, unsigned int mxlen){
static char s[LEN+1];
char * o=s;
int len;
s[LEN]=0;
for (len=(LEN-1);len>=(LEN-mxlen);len--){
@ -13,3 +13,46 @@ const char* IntToStrX(unsigned int num, unsigned int mxlen){
};
return &s[len+1];
};
#undef LEN
#define LEN 11
const char* IntToStr(int num, unsigned int mxlen, char flag){
static char s[LEN+1];
int len;
s[LEN]=0;
char neg=0;
if(num<0){
num=-num;
neg=1;
};
for (len=(LEN-1);len>=(LEN-mxlen);len--){
s[len]=(num%10)+'0';
num/=10;
};
len++;
if(!(flag&F_LONG)){
while(s[len]=='0' && len < (LEN-1))
len++;
}else if(!(flag&F_ZEROS)){
int x=len;
while(s[x]=='0' && x < (LEN-1)){
s[x]=' ';
x++;
};
}
if(neg==1){
len--;
s[len]='-';
}else if(flag&F_SPLUS){
len--;
s[len]='+';
}else if(flag&F_SSPACE){
s[len]=' ';
};
return &s[len];
};
#undef LEN