改写串口库,自带的库内存太大

本文介绍了一个用于格式化打印各种类型数据的函数库,包括整数、浮点数、字符串等不同类型的数据格式化输出方法。支持十进制、二进制、十六进制等多种进制的数字显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void print_ch(char s)
{
    SendData(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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值