您的位置 首页 知识

sprintf函数(STM8L051F3等小容量单片机sprintf函数的重构)

sprintf函数

   
     sprintf函数是字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。此函数在嵌入式编程中经常使用,给开发者带来了很大的方便,它被包含在stdio.h头文件之中。
    在我们的STM8L单片机群里经常看到有朋友咨询:STM8L051F3这类小容量的单片机开发中调用stdio.h头文件,会遇到内存溢出等问题,但是我们还想使用sprintf函数,那应该怎么办呢?我们可以自己编写一个简化版的sprintf来满足我们的需要即可,现将相关代码贴上来,希望给各位开发的朋友有点帮助!
#include <stdarg.h>

static void t_printchar(char **str, int c){extern int putchar(int c); if (str) {**str = c;++(*str);}else (void)putchar(c);}
#define PAD_RIGHT 1#define PAD_ZERO 2
static int t_prints(char **out, const char *string, int width, int pad){register int pc = 0, padchar = ‘ ‘;
if (width > 0) {register int len = 0;register const char *ptr;for (ptr = string; *ptr; ++ptr) ++len;if (len >= width) width = 0;else width -= len;if (pad & PAD_ZERO) padchar = ‘0’;}if (!(pad & PAD_RIGHT)) {for ( ; width > 0; –width) {t_printchar (out, padchar);++pc;}}for ( ; *string ; ++string) {t_printchar (out, *string);++pc;}for ( ; width > 0; –width) {t_printchar (out, padchar);++pc;}
return pc;}
/* the following should be enough for 32 bit int */#define PRINT_BUF_LEN 12
static int t_printi(char **out, int i, int b, int sg, int width, int pad, int letbase){char print_buf[PRINT_BUF_LEN];register char *s;register int t, neg = 0, pc = 0;register unsigned int u = i;
if (i == 0) {print_buf[0] = ‘0’;print_buf[1] = ”;return t_prints (out, print_buf, width, pad);}
if (sg && b == 10 && i < 0) {neg = 1;u = -i;}
s = print_buf + PRINT_BUF_LEN-1;*s = ”;
while (u) {t = u % b;if( t >= 10 )t += letbase – ‘0’ – 10;*–s = t + ‘0’;u /= b;}
if (neg) {if( width && (pad & PAD_ZERO) ) {t_printchar (out, ‘-‘);++pc;–width;}else {*–s = ‘-‘;}}
return pc + t_prints (out, s, width, pad);}
static int t_print( char **out, const char *format, va_list args ){register int width, pad;register int pc = 0;char scr[2];
for (; *format != 0; ++format) {if (*format == ‘%’) {++format;width = pad = 0;if (*format == ”) break;if (*format == ‘%’) goto out;if (*format == ‘-‘) {++format;pad = PAD_RIGHT;}while (*format == ‘0’) {++format;pad |= PAD_ZERO;}for ( ; *format >= ‘0’ && *format <= ‘9’; ++format) {width *= 10;width += *format – ‘0’;}if( *format == ‘s’ ) {register char *s = (char *)va_arg( args, int );pc += t_prints (out, s?s:”(null)”, width, pad);continue;}if( *format == ‘d’ ) {pc += t_printi (out, va_arg( args, int ), 10, 1, width, pad, ‘a’);continue;}if( *format == ‘x’ ) {pc += t_printi (out, va_arg( args, int ), 16, 0, width, pad, ‘a’);continue;}if( *format == ‘X’ ) {pc += t_printi (out, va_arg( args, int ), 16, 0, width, pad, ‘A’);continue;}if( *format == ‘u’ ) {pc += t_printi (out, va_arg( args, int ), 10, 0, width, pad, ‘a’);continue;}if( *format == ‘c’ ) {/* char are converted to int then pushed on the stack */scr[0] = (char)va_arg( args, int );scr[1] = ”;pc += t_prints (out, scr, width, pad);continue;}}else {out:t_printchar (out, *format);++pc;}}if (out) **out = ”;va_end( args );return pc;}
int t_printf(const char *format, …){        va_list args;                va_start( args, format );        return t_print( 0, format, args );}
int t_sprintf(char *out, const char *format, …){        va_list args;                va_start( args, format );        return t_print( &out, format, args );}

int t_snprintf( char *buf, unsigned int count, const char *format, … ){        va_list args;                ( void ) count;                va_start( args, format );        return t_print( &buf, format, args );}
我们调用上述代码中的t_sprintf函数即可。使用方法和C语言库里面的sprintf一样使用,但是占用单片机的空间却大大减少了,这给使用STM8L051F3的朋友带来了福音,现在来尝试一下吧!如果暂时用不着也可以收藏备用哦!完整文件请见QQ群文件!

文章版权归纳诺物联所有,未经允许不能应用于商业性应用及出版!欢迎关注我们的微信公众号“STM8单片机”,欢迎加入QQ学习群345283545,大家一起来学习STM8单片机。我们的官方淘宝店:https://lenchimcu.taobao.com

sprintf函数相关文章