我是靠谱客的博主 温婉巨人,最近开发中收集的这篇文章主要介绍【C/C++】使用DUMP8、DUMP16、DUMP32打印数据Buffer1.前言2.hal_print.c3.hal_print.h4.main.c5.测试效果6.资料下载,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1.前言
经常在编程时会遇到打印Data Buffer的情况,下面定义了DUMP8、DUMP16、DUMP32,很方便来打印Data Buffer里面的数据。
2.hal_print.c
#include "hal_print.h"
/**
****************************************************************************************
* @brief
function used to Print data in a fixed format
*
* @param[in]
fmt
(Print format)
*
size
(The size of the data)
*
count
(Length of data)
*
buffer
(The address where data is stored)
*
* @return
0(successful) or -1(fail)
****************************************************************************************
*/
int hal_print_dump(const char *fmt, unsigned int size,
unsigned int count, const void *buffer)
{
char buf[255];
unsigned int len=0,i=0;
switch( size )
{
case sizeof(uint32_t):
while(i<count && len<sizeof(buf))
{
len += HAL_SNPRINTF(&buf[len], sizeof(buf) - len, fmt, *(uint32_t *)((uint32_t *)buffer+i));
i++;
}
break;
case sizeof(uint16_t):
while(i<count && len<sizeof(buf))
{
len += HAL_SNPRINTF(&buf[len], sizeof(buf) - len, fmt, *(uint16_t *)((uint16_t *)buffer+i));
i++;
}
break;
case sizeof(uint8_t):
default:
while(i<count && len<sizeof(buf))
{
len += HAL_SNPRINTF(&buf[len], sizeof(buf) - len, fmt, *(uint8_t *)((uint8_t *)buffer+i));
i++;
}
break;
}
if (len + 1 < sizeof(buf)) {
buf[len++] = 'r';
}
if (len + 1 < sizeof(buf)) {
buf[len++] = 'n';
}
if (len + 1 < sizeof(buf)) {
buf[len++] = '