優(yōu)雅地打印 HEX 數(shù)據(jù)
“代碼片段。打印
在調(diào)試的何優(yōu)時(shí)候經(jīng)常要打印內(nèi)存里的數(shù)據(jù),來(lái)看看數(shù)據(jù)及格式是雅地否在預(yù)期范圍內(nèi);以及在調(diào)試二進(jìn)制協(xié)議的時(shí)候,經(jīng)常需要將協(xié)議包里的打印數(shù)據(jù)打印出來(lái),這個(gè)時(shí)候就會(huì)涉及到 HEX 數(shù)據(jù)的何優(yōu)展示問(wèn)題。
這篇文章就是雅地展示如何優(yōu)雅地打印 HEX 數(shù)據(jù)。
按照 BeyondCompare 形式打印 HEX 數(shù)據(jù)。打印
”
基礎(chǔ)版
代碼如下:
#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
void dump_hex(const uint8_t *buf, uint32_t size)
{
int i, j;
for (i = 0; i < size; i += 16)
{
printf("%08X: ", i);
for (j = 0; j < 16; j++)
{
if (i + j < size)
{
printf("%02X ", buf[i + j]);
}
else
{
printf(" ");
}
}
printf(" ");
for (j = 0; j < 16; j++)
{
if (i + j < size)
{
printf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
}
}
printf("\n");
}
}
測(cè)試代碼:
int main(void)
{
uint8_t i, buff[128];
for (i = 0; i < sizeof(buff); i++)
{
buff[i] = i;
}
dump_hex((const uint8_t *)buff, sizeof(buff), 16);
return 0;
}
測(cè)試效果:
00000000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................
00000010: 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ................
00000020: 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&'()*+,-./
00000030: 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 0123456789:;<=>?
00000040: 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFGHIJKLMNO
00000050: 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVWXYZ[\]^_
00000060: 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefghijklmno
00000070: 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvwxyz{ |}~.
升級(jí)版
相對(duì)基礎(chǔ)版本,何優(yōu)每行每 8 個(gè)數(shù)據(jù)多增加一個(gè)空格,雅地?cái)?shù)據(jù)展示更加直觀。打印
#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
/**
* dump_hex
*
* @brief dump data in hex format
*
* @param buf: User buffer
* @param size: Dump data size
* @param number: The number of outputs per line
*
* @return void
*/
void dump_hex(const uint8_t *buf, uint32_t size, uint32_t number)
{
int i, j;
for (i = 0; i < size; i += number)
{
printf("%08X: ", i);
for (j = 0; j < number; j++)
{
if (j % 8 == 0)
{
printf(" ");
}
if (i + j < size)
printf("%02X ", buf[i + j]);
else
printf(" ");
}
printf(" ");
for (j = 0; j < number; j++)
{
if (i + j < size)
{
printf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
}
}
printf("\n");
}
}
測(cè)試結(jié)果:
00000000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................
00000010: 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ................
00000020: 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&'()*+,-./
00000030: 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 0123456789:;<=>?
00000040: 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFGHIJKLMNO
00000050: 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVWXYZ[\]^_
00000060: 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefghijklmno
00000070: 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvwxyz{ |}~.
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問(wèn)題,請(qǐng)聯(lián)系我們,謝謝!