I'd written an article [http://blog.csdn.net/wxqee/article/details/7644831] about this title. Here is the upgrade version as C++. It has been tested in Linux, you can just copy & paste  the source codes into your project, then simply use as following to query a text in Hex style.

string str, hexstr;
str = "dasjfkldjsafl; jkdfdaifeoweqrnealknf1u4908278593721";

wx::TraceBin::tracebin (str, hexstr);
std::cout << hexstr;

Very simple, right? Here are the source codes.

tracebin_h

#ifndef _WX_TRACEBIN_H
#define _WX_TRACEBIN_H

#include <string>

namespace wx {

class BinaryTracer
{
private:
    static const unsigned int step = 16;

private:
    static char viewChar (const char ch);

    static std::string viewHead  (int i);
    static std::string viewHexes (const std::string &str);
    static std::string viewChars (const std::string &str);

public:

    
    // 0         1         2         3         4         5         6         7      
    // 01234567890123456789012345678901234567890123456789012345678901234567890123456
    // -----------------------------------------------------------------------------
    // 0x000000  61 62 63 64 65 66 67 68  69 6A 6B 6C 6D 6E 6F 70   abcdefghijklmnop
    // 0x000010  71 72 73 74 75 76 77 78  79 7A 30 31 32 33 34 35   qrstuvwxyz012345
    // 0x000020  36 37 38 39                                        6789
    

    static void tracebin (const std::string &str, std::string &hex);

    // \brief trace file.
    // [in]  filename     string, file name.
    // [out] hex          string, append to hex.
    static void tracebin_f (const std::string filename, std::string &hex);

    // \brief trace memory.
    // [in]  memory       const char *, memory ready to trace.
    // [in]  length       unsigned int, size of memory.
    // [out] hex          string, append to hex.
    static void tracebin_m (const void * memory, unsigned int length, std::string &hex);

};

};

#endif

tracebin_cpp

#include "tracebin.h"

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <iostream>
#include <fstream>

namespace wx {

char BinaryTracer::viewChar (const char ch)
{
    if (ch >= 33 && ch <= 126)
    {
        return ch;
    }
    else
    {
        return '.';
    }

}

std::string BinaryTracer::viewHead  (int i)
{
    enum { HeaderLength = 9 };

    char buf[HeaderLength];
    memset (buf, 0, HeaderLength);

    sprintf (buf, "0x%06X  ", i);

    return buf;
}

std::string BinaryTracer::viewHexes (const std::string &str)
{
    enum { HexesLength = step * 3 + 2 + 1};

    char buf[HexesLength];
    memset (buf, ' ', HexesLength);
    buf[HexesLength - 1] = '\0';

    int currentPosition = 0;
    for (unsigned int i = 0; i < str.length(); i++)
    {
        sprintf (buf + currentPosition, "%02X ", (unsigned char) str[i]);
        currentPosition += 3;

        if (i + 1 == step / 2)
        {
            sprintf (buf + currentPosition, " ");
            currentPosition += 1;
        }
    }
    
    if (currentPosition + 1 < HexesLength)
    {
        buf[currentPosition] = ' ';
    }

    return buf;
    
}

std::string BinaryTracer::viewChars (const std::string &str)
{
    char buf[step + 1];
    memset (buf, 0, step + 1);

    for (unsigned int i = 0; i < str.length(); i++)
    {
        sprintf (buf + i, "%c", viewChar(str[i]));
    }
    buf[str.length()] = '\0';
    return buf;
}

void BinaryTracer::tracebin (const std::string &str, std::string &hex)
{
    for (unsigned int i = 0; i < str.length(); i += step)
    {
        std::string currentLine = str.substr (i, step);

        hex += viewHead (i);
        hex += viewHexes (currentLine);
        hex += viewChars (currentLine);
        hex += "\n";
    }
}

void BinaryTracer::tracebin_f (const std::string filename, std::string &hex)
{
    std::string str;
    std::ifstream infile;

    infile.open (filename.c_str(), std::ifstream::in);

    for (unsigned char ch = infile.get(); infile.good(); ch = infile.get())
    {
        str += ch;
    }

    infile.close();

    wx::BinaryTracer::tracebin (str, hex);
}

void BinaryTracer::tracebin_m (const void * memory, unsigned int length, std::string &hex)
{
    std::string str;
    str.append ((char*)memory, length);
    tracebin (str, hex);
}

};

Makefile

NAME        = tracebin
VERSION     = 1.0.0
RELEASE     = 01

ARNAME      = lib$(NAME).a


OBJS        = tracebin.o

CXXFLAGS    = -Wall -g
CXXFLAGS   += -I.
CPPFLAGS    = -DNAME=\"$(NAME)\" -DVERSION=\"$(VERSION)\" -DRELEASE=\"$(RELEASE)\"
CPPFLAGS   += -D_DEBUG


.PHONY: all clean

all: $(ARNAME) test$(NAME)

test$(NAME): main.o
	$(CXX) $(CPPFLAGS) -L. $^ -o $@ -ltracebin

$(ARNAME): $(OBJS)
	$(AR) -r $@ $^

%.o: %.cpp
	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@

clean:
	$(RM) $(ARNAME) $(OBJS) \
	test$(NAME) main.o

main_cpp (Demo)

#include <string.h>

#include <iostream>
#include <fstream>

#include "tracebin.h"

void test001 ();
void test002 ();
void test003 ();

int main (int argc, char **argv)
{

    // Trace a text memory
    test001();

    // Trace a binary or text file
    test002();

    // Trace a memory.
    test003();

    return 0;
}

void test001 ()
{
    std::string str;
    std::string hex;

    hex.clear();
    std::cout << "Trace a std::string." << std::endl;

    str = "abcdefghijklmnopqrstuvwxyz0123456789";
    wx::BinaryTracer::tracebin (str, hex);
    std::cout << hex << std::endl;
}

void test002 ()
{
    std::string str;
    std::string hex;

    std::cout << "Trace a file." << std::endl;

    wx::BinaryTracer::tracebin_f ("main.cpp", hex);
    std::cout << hex << std::endl;
}

void test003 ()
{
    std::string str;
    std::string hex;

    std::cout << "Trace a buf." << std::endl;

    struct MyTest_M {
        int a;
        int b;
        char c;
        char d[20];
    } test_buf;
    test_buf.a = 1;
    test_buf.b = 2;
    test_buf.c = 'a';
    strcpy (test_buf.d, "abcd你好啊abc");
    wx::BinaryTracer::tracebin_m (&test_buf, sizeof (MyTest_M), hex);
    std::cout << hex;
}

Compile & Run Demo (testtracebin)

$ ./testtracebin.exe
Trace a std::string.
0x000000  61 62 63 64 65 66 67 68  69 6A 6B 6C 6D 6E 6F 70  abcdefghijklmnop
0x000010  71 72 73 74 75 76 77 78  79 7A 30 31 32 33 34 35  qrstuvwxyz012345
0x000020  36 37 38 39                                       6789

Trace a file.
0x000000  23 69 6E 63 6C 75 64 65  20 3C 73 74 72 69 6E 67  #include.<string
0x000010  2E 68 3E 0A 0A 23 69 6E  63 6C 75 64 65 20 3C 69  .h>..#include.<i
0x000020  6F 73 74 72 65 61 6D 3E  0A 23 69 6E 63 6C 75 64  ostream>.#includ
0x000030  65 20 3C 66 73 74 72 65  61 6D 3E 0A 0A 23 69 6E  e.<fstream>..#in
0x000040  63 6C 75 64 65 20 22 74  72 61 63 65 62 69 6E 2E  clude."tracebin.
0x000050  68 22 0A 0A 76 6F 69 64  20 74 65 73 74 30 30 31  h"..void.test001
0x000060  20 28 29 3B 0A 76 6F 69  64 20 74 65 73 74 30 30  .();.void.test00
0x000070  32 20 28 29 3B 0A 76 6F  69 64 20 74 65 73 74 30  2.();.void.test0
0x000080  30 33 20 28 29 3B 0A 0A  69 6E 74 20 6D 61 69 6E  03.();..int.main
0x000090  20 28 69 6E 74 20 61 72  67 63 2C 20 63 68 61 72  .(int.argc,.char
0x0000A0  20 2A 2A 61 72 67 76 29  0A 7B 0A 0A 20 20 20 20  .**argv).{......
0x0000B0  2F 2F 20 54 72 61 63 65  20 61 20 74 65 78 74 20  //.Trace.a.text.
0x0000C0  6D 65 6D 6F 72 79 0A 20  20 20 20 74 65 73 74 30  memory.....test0
0x0000D0  30 31 28 29 3B 0A 0A 20  20 20 20 2F 2F 20 54 72  01();......//.Tr
0x0000E0  61 63 65 20 61 20 62 69  6E 61 72 79 20 6F 72 20  ace.a.binary.or.
0x0000F0  74 65 78 74 20 66 69 6C  65 0A 20 20 20 20 74 65  text.file.....te
0x000100  73 74 30 30 32 28 29 3B  0A 0A 20 20 20 20 2F 2F  st002();......//
0x000110  20 54 72 61 63 65 20 61  20 6D 65 6D 6F 72 79 2E  .Trace.a.memory.
0x000120  0A 20 20 20 20 74 65 73  74 30 30 33 28 29 3B 0A  .....test003();.
0x000130  0A 20 20 20 20 72 65 74  75 72 6E 20 30 3B 0A 7D  .....return.0;.}
0x000140  0A 0A 76 6F 69 64 20 74  65 73 74 30 30 31 20 28  ..void.test001.(
0x000150  29 0A 7B 0A 20 20 20 20  73 74 64 3A 3A 73 74 72  ).{.....std::str
0x000160  69 6E 67 20 73 74 72 3B  0A 20 20 20 20 73 74 64  ing.str;.....std
0x000170  3A 3A 73 74 72 69 6E 67  20 68 65 78 3B 0A 0A 20  ::string.hex;...
0x000180  20 20 20 68 65 78 2E 63  6C 65 61 72 28 29 3B 0A  ...hex.clear();.
0x000190  20 20 20 20 73 74 64 3A  3A 63 6F 75 74 20 3C 3C  ....std::cout.<<
0x0001A0  20 22 54 72 61 63 65 20  61 20 73 74 64 3A 3A 73  ."Trace.a.std::s
0x0001B0  74 72 69 6E 67 2E 22 20  3C 3C 20 73 74 64 3A 3A  tring.".<<.std::
0x0001C0  65 6E 64 6C 3B 0A 0A 20  20 20 20 73 74 72 20 3D  endl;......str.=
0x0001D0  20 22 61 62 63 64 65 66  67 68 69 6A 6B 6C 6D 6E  ."abcdefghijklmn
0x0001E0  6F 70 71 72 73 74 75 76  77 78 79 7A 30 31 32 33  opqrstuvwxyz0123
0x0001F0  34 35 36 37 38 39 22 3B  0A 20 20 20 20 77 78 3A  456789";.....wx:
0x000200  3A 42 69 6E 61 72 79 54  72 61 63 65 72 3A 3A 74  :BinaryTracer::t
0x000210  72 61 63 65 62 69 6E 20  28 73 74 72 2C 20 68 65  racebin.(str,.he
0x000220  78 29 3B 0A 20 20 20 20  73 74 64 3A 3A 63 6F 75  x);.....std::cou
0x000230  74 20 3C 3C 20 68 65 78  20 3C 3C 20 73 74 64 3A  t.<<.hex.<<.std:
0x000240  3A 65 6E 64 6C 3B 0A 7D  0A 0A 76 6F 69 64 20 74  :endl;.}..void.t
0x000250  65 73 74 30 30 32 20 28  29 0A 7B 0A 20 20 20 20  est002.().{.....
0x000260  73 74 64 3A 3A 73 74 72  69 6E 67 20 73 74 72 3B  std::string.str;
0x000270  0A 20 20 20 20 73 74 64  3A 3A 73 74 72 69 6E 67  .....std::string
0x000280  20 68 65 78 3B 0A 0A 20  20 20 20 73 74 64 3A 3A  .hex;......std::
0x000290  63 6F 75 74 20 3C 3C 20  22 54 72 61 63 65 20 61  cout.<<."Trace.a
0x0002A0  20 66 69 6C 65 2E 22 20  3C 3C 20 73 74 64 3A 3A  .file.".<<.std::
0x0002B0  65 6E 64 6C 3B 0A 0A 20  20 20 20 77 78 3A 3A 42  endl;......wx::B
0x0002C0  69 6E 61 72 79 54 72 61  63 65 72 3A 3A 74 72 61  inaryTracer::tra
0x0002D0  63 65 62 69 6E 5F 66 20  28 22 6D 61 69 6E 2E 63  cebin_f.("main.c
0x0002E0  70 70 22 2C 20 68 65 78  29 3B 0A 20 20 20 20 73  pp",.hex);.....s
0x0002F0  74 64 3A 3A 63 6F 75 74  20 3C 3C 20 68 65 78 20  td::cout.<<.hex.
0x000300  3C 3C 20 73 74 64 3A 3A  65 6E 64 6C 3B 0A 7D 0A  <<.std::endl;.}.
0x000310  0A 76 6F 69 64 20 74 65  73 74 30 30 33 20 28 29  .void.test003.()
0x000320  0A 7B 0A 20 20 20 20 73  74 64 3A 3A 73 74 72 69  .{.....std::stri
0x000330  6E 67 20 73 74 72 3B 0A  20 20 20 20 73 74 64 3A  ng.str;.....std:
0x000340  3A 73 74 72 69 6E 67 20  68 65 78 3B 0A 0A 20 20  :string.hex;....
0x000350  20 20 73 74 64 3A 3A 63  6F 75 74 20 3C 3C 20 22  ..std::cout.<<."
0x000360  54 72 61 63 65 20 61 20  62 75 66 2E 22 20 3C 3C  Trace.a.buf.".<<
0x000370  20 73 74 64 3A 3A 65 6E  64 6C 3B 0A 0A 20 20 20  .std::endl;.....
0x000380  20 73 74 72 75 63 74 20  4D 79 54 65 73 74 5F 4D  .struct.MyTest_M
0x000390  20 7B 0A 20 20 20 20 20  20 20 20 69 6E 74 20 61  .{.........int.a
0x0003A0  3B 0A 20 20 20 20 20 20  20 20 69 6E 74 20 62 3B  ;.........int.b;
0x0003B0  0A 20 20 20 20 20 20 20  20 63 68 61 72 20 63 3B  .........char.c;
0x0003C0  0A 20 20 20 20 20 20 20  20 63 68 61 72 20 64 5B  .........char.d[
0x0003D0  32 30 5D 3B 0A 20 20 20  20 7D 20 74 65 73 74 5F  20];.....}.test_
0x0003E0  62 75 66 3B 0A 20 20 20  20 74 65 73 74 5F 62 75  buf;.....test_bu
0x0003F0  66 2E 61 20 3D 20 31 3B  0A 20 20 20 20 74 65 73  f.a.=.1;.....tes
0x000400  74 5F 62 75 66 2E 62 20  3D 20 32 3B 0A 20 20 20  t_buf.b.=.2;....
0x000410  20 74 65 73 74 5F 62 75  66 2E 63 20 3D 20 27 61  .test_buf.c.=.'a
0x000420  27 3B 0A 20 20 20 20 73  74 72 63 70 79 20 28 74  ';.....strcpy.(t
0x000430  65 73 74 5F 62 75 66 2E  64 2C 20 22 61 62 63 64  est_buf.d,."abcd
0x000440  E4 BD A0 E5 A5 BD E5 95  8A 61 62 63 22 29 3B 0A  .........abc");.
0x000450  20 20 20 20 77 78 3A 3A  42 69 6E 61 72 79 54 72  ....wx::BinaryTr
0x000460  61 63 65 72 3A 3A 74 72  61 63 65 62 69 6E 5F 6D  acer::tracebin_m
0x000470  20 28 26 74 65 73 74 5F  62 75 66 2C 20 73 69 7A  .(&test_buf,.siz
0x000480  65 6F 66 20 28 4D 79 54  65 73 74 5F 4D 29 2C 20  eof.(MyTest_M),.
0x000490  68 65 78 29 3B 0A 20 20  20 20 73 74 64 3A 3A 63  hex);.....std::c
0x0004A0  6F 75 74 20 3C 3C 20 68  65 78 3B 0A 7D 0A 0A 0A  out.<<.hex;.}...

Trace a buf.
0x000000  01 00 00 00 02 00 00 00  61 61 62 63 64 E4 BD A0  ........aabcd...
0x000010  E5 A5 BD E5 95 8A 61 62  63 00 04 80 44 01 E7 49  ......abc...D..I

Very useful right?

History:

2013-02-03 

Interfaces that libtracebin.a extras are: 
- static void tracebin (const std::string &str, std::string &hex);
- static void tracebin_f (const std::string filename, std::string &hex);
- static void tracebin_m (const void * memory, unsigned int length, std::string &hex);



Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐