|
楼主 |
发表于 2017-11-1 11:43:28
|
显示全部楼层
自己搞了一个
- void print_ch(char s)
- {
- UART_Putc(s);
- // buf[pp] = s;
- // pp++;
- }
- void print_chs(char*s)
- {
- while(*s)
- {
- print_ch(*s++);
- }
- }
- //字符串
- void print_str(char* str)
- {
- print_chs(str);
- }
- //打印2进制数,打印2进制数,除0和负数
- void print_bin_0(int bin)
- {
- if(bin == 0)
- {
- print_chs("0b");
- return;
- }
- print_bin_0(bin/2);
- print_ch( (char)(bin%2 + '0'));
- }
- //打印2进制数
- void print_bin(int bin)
- {
- if(bin == 0)
- {
- print_chs("0b0");
- return;
- }
- if(bin<0)
- {
- print_ch('-');
- bin = 0-bin;
- }
- print_bin_0(bin);
- }
- //打印10进制数,除0和负数
- void print_dec_0(long dec)
- {
- if(dec==0)
- {
- return;
- }
- print_dec_0(dec/10);
- print_ch( (char)(dec%10 + '0'));
- }
- //打印10进制数
- void print_dec(long dec)
- {
-
- if(dec==0)
- {
- print_ch('0');
- return;
- }
- if(dec<0)
- {
- print_ch('-');
- dec = 0-dec;
- }
- print_dec_0(dec);
- }
- //打印float小数
- void print_flt(double flt)
- {
- //int icnt = 0;
- long tmpint = 0;
- unsigned char i = 0;
- if(flt<0)
- {
- print_ch('-');
- flt = 0-flt;
- }
- else if(flt==0)
- {
- print_ch('0');
- return;
- }
- tmpint = (int)flt;
- if(tmpint>=1) print_dec(tmpint);
- else if(flt>0) print_ch('0');
- flt = flt - tmpint;
- if((flt!=0)&&(flt<1))
- {
- print_ch('.');
- tmpint = (long)(flt * 1000000);
-
- for(i=6;i>0;i--)
- {
- flt *= 10;
- if((char)(flt)%10 == 0) print_ch('0');
- else break ;
- }
-
- for(i=6;i>0;i--)
- {
- if(tmpint%10 == 0) tmpint /= 10;
- else break ;
- }
- print_dec(tmpint);
- }
- }
- //以16进制打印,除0和负数
- void print_hex_0(long hex)
- {
- if(hex==0)
- {
- print_chs("0x");
- return;
- }
- print_hex_0(hex/16);
- hex %= 16;
- if(hex < 10)
- {
- print_ch((char)((hex%16) + '0'));
- }
- else
- {
- print_ch((char)((hex%16) - 10 + 'A' ));
- }
- }
- //以16进制打印
- void print_hex(long hex)
- {
- if(hex==0)
- {
- print_chs("0x0");
- return;
- }
- if(hex<0)
- {
- print_ch('-');
- hex = 0-hex;
- }
- print_hex_0(hex);
- }
- void print(char* fmt, ...)
- {
- double vargflt = 0;
- int vargint = 0;
- char* vargpch = NULL;
- char vargch = 0;
- char* pfmt = NULL;
- va_list vp;
- va_start(vp, fmt);
- pfmt = fmt;
- while(*pfmt)
- {
- if(*pfmt == '%')
- {
- switch(*(++pfmt))
- {
-
- case 'c':
- vargch = va_arg(vp, int);
- /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
- mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
- print_ch(vargch);
- break;
- case 'd':
- case 'i':
- vargint = va_arg(vp, int);
- print_dec(vargint);
- break;
- case 'f':
- vargflt = va_arg(vp, double);
- /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
- mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
- print_flt(vargflt);
- break;
- case 's':
- vargpch = va_arg(vp, char*);
- print_str(vargpch);
- break;
- case 'b':
- case 'B':
- vargint = va_arg(vp, int);
- print_bin(vargint);
- break;
- case 'x':
- case 'X':
- vargint = va_arg(vp, int);
- print_hex(vargint);
- break;
- case '%':
- print_ch('%');
- break;
- default:
- break;
- }
- pfmt++;
- }
- else
- {
- print_ch(*pfmt++);
- }
- }
- va_end(vp);
- }
复制代码 |
|