[C ++] [Source Code] tracebin: Get text as hex string.

Source: Internet
Author: User

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        = tracebinVERSION     = 1.0.0RELEASE     = 01ARNAME      = lib$(NAME).aOBJS        = tracebin.oCXXFLAGS    = -Wall -gCXXFLAGS   += -I.CPPFLAGS    = -DNAME=\"$(NAME)\" -DVERSION=\"$(VERSION)\" -DRELEASE=\"$(RELEASE)\"CPPFLAGS   += -D_DEBUG.PHONY: all cleanall: $(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 Compile & run demo (testtracebin)

$ ./testtracebin.exeTrace a std::string.0x000000  61 62 63 64 65 66 67 68  69 6A 6B 6C 6D 6E 6F 70  abcdefghijklmnop0x000010  71 72 73 74 75 76 77 78  79 7A 30 31 32 33 34 35  qrstuvwxyz0123450x000020  36 37 38 39                                       6789Trace a file.0x000000  23 69 6E 63 6C 75 64 65  20 3C 73 74 72 69 6E 67  #include.<string0x000010  2E 68 3E 0A 0A 23 69 6E  63 6C 75 64 65 20 3C 69  .h>..#include.<i0x000020  6F 73 74 72 65 61 6D 3E  0A 23 69 6E 63 6C 75 64  ostream>.#includ0x000030  65 20 3C 66 73 74 72 65  61 6D 3E 0A 0A 23 69 6E  e.<fstream>..#in0x000040  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.test0010x000060  20 28 29 3B 0A 76 6F 69  64 20 74 65 73 74 30 30  .();.void.test000x000070  32 20 28 29 3B 0A 76 6F  69 64 20 74 65 73 74 30  2.();.void.test00x000080  30 33 20 28 29 3B 0A 0A  69 6E 74 20 6D 61 69 6E  03.();..int.main0x000090  20 28 69 6E 74 20 61 72  67 63 2C 20 63 68 61 72  .(int.argc,.char0x0000A0  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.....test00x0000D0  30 31 28 29 3B 0A 0A 20  20 20 20 2F 2F 20 54 72  01();......//.Tr0x0000E0  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.....te0x000100  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::str0x000160  69 6E 67 20 73 74 72 3B  0A 20 20 20 20 73 74 64  ing.str;.....std0x000170  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::s0x0001B0  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  ."abcdefghijklmn0x0001E0  6F 70 71 72 73 74 75 76  77 78 79 7A 30 31 32 33  opqrstuvwxyz01230x0001F0  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::t0x000210  72 61 63 65 62 69 6E 20  28 73 74 72 2C 20 68 65  racebin.(str,.he0x000220  78 29 3B 0A 20 20 20 20  73 74 64 3A 3A 63 6F 75  x);.....std::cou0x000230  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.t0x000250  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::string0x000280  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.a0x0002A0  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::B0x0002C0  69 6E 61 72 79 54 72 61  63 65 72 3A 3A 74 72 61  inaryTracer::tra0x0002D0  63 65 62 69 6E 5F 66 20  28 22 6D 61 69 6E 2E 63  cebin_f.("main.c0x0002E0  70 70 22 2C 20 68 65 78  29 3B 0A 20 20 20 20 73  pp",.hex);.....s0x0002F0  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::stri0x000330  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_M0x000390  20 7B 0A 20 20 20 20 20  20 20 20 69 6E 74 20 61  .{.........int.a0x0003A0  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_bu0x0003F0  66 2E 61 20 3D 20 31 3B  0A 20 20 20 20 74 65 73  f.a.=.1;.....tes0x000400  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.=.'a0x000420  27 3B 0A 20 20 20 20 73  74 72 63 70 79 20 28 74  ';.....strcpy.(t0x000430  65 73 74 5F 62 75 66 2E  64 2C 20 22 61 62 63 64  est_buf.d,."abcd0x000440  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::BinaryTr0x000460  61 63 65 72 3A 3A 74 72  61 63 65 62 69 6E 5F 6D  acer::tracebin_m0x000470  20 28 26 74 65 73 74 5F  62 75 66 2C 20 73 69 7A  .(&test_buf,.siz0x000480  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::c0x0004A0  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 );


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.