-
Notifications
You must be signed in to change notification settings - Fork 1
/
HexDump.h
executable file
·54 lines (46 loc) · 1.17 KB
/
HexDump.h
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
//
// HexDump.h
// AmrRecoderAndPlayer
//
// Created by lu gang on 8/27/13.
#ifndef AmrRecoderAndPlayer_HexDump_h
#define AmrRecoderAndPlayer_HexDump_h
#include <stdio.h>
#include <SP.h>
template <class T>
void displayHexBin(const T& v, bool hex = true)
{
const unsigned char c2h[] = "0123456789ABCDEF";
const unsigned char c2b[] = "01";
unsigned char* p = (unsigned char*)&v;
char* buf = new char [sizeof(T)*2+1];
char* ptmp = buf;
if(hex)
{
p = p + sizeof(T)-1;
for (size_t i = 0; i < sizeof(T); i++, --p)
{
*buf++ = c2h[*p >> 4];
*buf++ = c2h[*p & 0x0F];
}
*buf = '\0';
SP::printf("%s", ptmp);
delete [] ptmp;
}
else
{
p = (unsigned char*)&v;
p = p + sizeof(T)-1;
ptmp = buf = new char [sizeof(T)*8+1];
for (size_t i = 0; i < sizeof(T); i++, --p)
{
for (int j = 0; j < 8; j++)
*buf++ = c2b[(*p >> (7-j)) & 0x1];
}
*buf = '\0';
SP::printf("%s", ptmp);
delete [] ptmp;
}
}
void bytes2HexS(const unsigned char* src, size_t size);
#endif