From 06ac802ee5187d5c3b0e97733f01cdcf359659a9 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sun, 31 Jul 2011 00:53:14 +0200 Subject: [PATCH] More abstracted num to string functions --- firmware/basic/basic.h | 5 +++++ firmware/basic/itoa.c | 47 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index 185c9ed..c4ec7bb 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -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 diff --git a/firmware/basic/itoa.c b/firmware/basic/itoa.c index 9cc8de5..abaef8f 100644 --- a/firmware/basic/itoa.c +++ b/firmware/basic/itoa.c @@ -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