C语言实现Printf函数功能,并输出字符串至char数组

Printf函数实现数据按照特定格式从串口等设备输出,但是有些时候想要其输出到自定义的buf[]数组中,方便的实现不同种类的数据类型转换为字符串类型的数据。

      代码在Vscode可运行

#include <iostream>
#include "stdarg.h"
#include "stdlib.h"
#include "stdio.h"
struct stringPrint
{
	uint16_t buf_len;
	char strbuf[128];
};
struct stringPrint consoleBuf;
void write_char(char data)
{
	consoleBuf.strbuf[consoleBuf.buf_len++] = data;
}
void write_string(const char* string)
{
	while(*string)
	{
		write_char(*string++);
	}
}
char *int_to_str(long number, char base)
{
	unsigned long pos_number = 0;
	char		  neg_sign   = 0;
	const char	  digits[17] = "0123456789ABCDEF";
	static char   buffer[17] = "                ";
	char*		  buffer_ptr = &buffer[16];

	if((number < 0) && (base == 10))
	{
		neg_sign = 1;
		pos_number = -1 * number;
	}
	else
	{
		pos_number = (unsigned long)number;
	}
	do
	{
		*buffer_ptr-- = digits[pos_number % base];
		pos_number /= base;
	}
	while(pos_number != 0);
	if(neg_sign)
	{
		*buffer_ptr-- = '-';
	}
	*buffer_ptr++;

	return buffer_ptr;
}

void consolePrint(char* str, ...)
{
	char*		  string_ptr;
	char		  first_param  = 0;
	char		  second_param = 0;
	unsigned long x_mask	   = 0;
	unsigned long d_mask	   = 0;
	char		  ch_number	   = 0;
	unsigned long multiplier   = 1;
	char*		  str_arg;
	long		  long_arg;
	double		  double_arg;
	va_list		  argp;
    consoleBuf.buf_len = 0;
	va_start(argp, str);
	for(string_ptr = str; *string_ptr != '\0'; string_ptr++)
	{
		if(*string_ptr!='%')
		{
			write_char(*string_ptr);
			continue;
		}
		string_ptr++;
		first_param = 0;
		while((*string_ptr >= 0x30) & (*string_ptr <= 0x39))
		{
			first_param *= 10;
			first_param += (*string_ptr - 0x30);
			string_ptr++;
		}
		if(*string_ptr == '.')
		{
			string_ptr++;
			second_param = 0;
			while((*string_ptr >= 0x30) & (*string_ptr <= 0x39))
			{
				second_param *= 10;
				second_param += (*string_ptr - 0x30);
				string_ptr++;
			}
		}
		switch(*string_ptr)
		{
		case 'c':
			long_arg = va_arg(argp, long);
			write_char((char)long_arg);
			break;
		case 's':
			str_arg = va_arg(argp, char*);
			write_string(str_arg);
			break;
		case 'd':
			long_arg = va_arg(argp, long);
			write_string(int_to_str(long_arg, 10));
			break;
		case 'x':
			long_arg = va_arg(argp, long);
			x_mask = 268435456;
			ch_number = 8;
			while(x_mask > long_arg)
			{
				x_mask /= 16;
				ch_number--;
			}
			while(ch_number < first_param)
			{
				write_char('0');
				ch_number++;
			}
			if(long_arg != 0)
			{
				write_string(int_to_str(long_arg, 16));
			}
			break;
		case 'f':
			double_arg = va_arg(argp, double);
			multiplier = 1;
			if(second_param == 0)
			{
				second_param = 3;
			}
			ch_number = second_param;
			while(ch_number > 0)
			{
				multiplier *= 10;
				ch_number--;
			}
			double_arg *= multiplier;
			if(double_arg < 0)
			{
				double_arg *= -1;
				write_char('-');
			}
			long_arg = (long)double_arg;
			write_string(int_to_str((long_arg / multiplier), 10));
			write_char('.');
			d_mask = 1000000000;
			ch_number = 10;
			while(d_mask > (long)(long_arg % multiplier))
			{
				d_mask /= 10;
				ch_number--;
			}
			while(ch_number < second_param)
			{
				write_char('0');
				ch_number++;
			}
			if((long_arg % multiplier) != 0)
			{
				write_string(int_to_str((long_arg % multiplier), 10));
			}
			break;
		}
	}
	va_end(argp);
}

int main()
{

    std::cout<<"consolePrint test"<<std::endl;
    consolePrint("test=%x %d %f %s\r\n",15,123,456.78,"helloWord");
    std::cout<<"buf_len = "<< consoleBuf.buf_len<< " "<<consoleBuf.strbuf<<std::endl;
    system("pause");
    return 0;
}

输出结果展示:

C语言实现Printf函数功能,并输出字符串至char数组

 

上一篇:一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER


下一篇:从零开始学并发四.CountDownLatch