我是靠谱客的博主 温婉巨人,这篇文章主要介绍【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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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++] = ''; } HAL_PRINT("%s",buf); return len; }

3.hal_print.h

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef __HAL_PRINT_H__ #define __HAL_PRINT_H__ /******************************************************************************************* * @brief Header files and macro definitions differ on each platform. * *******************************************************************************************/ #include "stdio.h" #include "stdint.h" #define HAL_SNPRINTF snprintf #define HAL_PRINT printf /*******************************************************************************************/ int hal_print_dump(const char *fmt, unsigned int size, unsigned int count, const void *buffer); #define DUMP8(str, buf, cnt) hal_print_dump(str, sizeof(uint8_t), cnt, buf) #define DUMP16(str, buf, cnt) hal_print_dump(str, sizeof(uint16_t), cnt, buf) #define DUMP32(str, buf, cnt) hal_print_dump(str, sizeof(uint32_t), cnt, buf) #endif // __HAL_PRINT_H__

4.main.c

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdio.h" #include "stdint.h" #include "hal_print.h" uint8_t dataBuf[52]; int main(void) { uint8_t i; // init data for(i = 0; i < sizeof(dataBuf); i++) { dataBuf[i] = i + 1; } // print data printf("DUMP8:rn"); DUMP8("0x%02x ", dataBuf, 30); printf("rnDUMP16:rn"); DUMP16("0x%04x ", dataBuf, 20); printf("rnDUMP32:rn"); DUMP32("0x%08x ", dataBuf, 10); return 0; }

5.测试效果

在这里插入图片描述

6.资料下载

完整的代码下载地址如下:
https://download.csdn.net/download/ZHONGCAI0901/15482516

最后

以上就是温婉巨人最近收集整理的关于【C/C++】使用DUMP8、DUMP16、DUMP32打印数据Buffer1.前言2.hal_print.c3.hal_print.h4.main.c5.测试效果6.资料下载的全部内容,更多相关【C/C++】使用DUMP8、DUMP16、DUMP32打印数据Buffer1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部