内存数据的十六进制Print

以十六进制输出一块内存的内容

更新时间:2011-11-11 15:52:04   来源:未知   作者:goldpony   浏览:2337次
下面代码是编写网络通信程序时经常要用到的一个函数,用来将内存中的内容按十六进制格式输出。包括使用例子。 //needed for printf() #include stdio.h //needed for strlen() #include string.h // prints the contents
      下面代码是编写网络通信程序时经常要用到的一个函数,用来将内存中的内容按十六进制格式输出。包括使用例子。

海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
      //needed for printf()
      #include <stdio.h>

      //needed for strlen()
      #include <string.h>

      // prints the contents of memory in hex and ascii.
      // starts at the location of the pointer "start"
      // prints "length" bytes of memory.
      void Print_Memory(const unsigned char * start, unsigned int length)
      {
          //create row, col, and i.  Set i to 0
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
          int row, col, i = 0;

          //iterate through the rows, which will be 16 bytes of memory wide
          for(row = 0; (i + 1) < length; row++)
          {
              //print hex representation
              for(col = 0; col<16; col++)
              {
                  //calculate the current index
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
                  i = row*16+col;

                  //divides a row of 16 into two columns of 8
                  if(col==8)
                  {
                      printf(" ");
                  }

                  //print the hex value if the current index is in range.
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
                  if(i<length)
                  {
                      printf("%02X", start[i]);
                  }
                  //print a blank if the current index is past the end
                  else
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
                  {
                      printf("  ");
                  }

                  //print a space to keep the values separate
                  printf(" ");
              }

              //create a vertial seperator between hex and ascii representations
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
              printf(" ");

              //print ascii representation
              for(col = 0; col<16; col++)
              {
                  //calculate the current index
                  i = row*16+col;

                  //divides a row of 16 into two coumns of 8
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
                  if(col==8)
                  {
                      printf("  ");
                  }

                  //print the value if it is in range
                  if(i<length)
                  {
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
                      //print the ascii value if applicable
                      if(start[i]>0x20 && start[i]<0x7F)  //A-Z
                      {
                          printf("%c", start[i]);
                      }
                      //print a period if the value is not printable
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
                      else
                      {
                          printf(".");
                      }
                  }
                  //nothing else to print, so break out of this for loop
                  else
海姹网(网址:http://www.seacha.com),标签:以十六进制输出一块内存的内容, 十六进制,printf,Show_Memory
                  {
                      break;
                  }
              }

              //create a new row
              printf("\n");
          }
      }

      // Prints the contents of memory in hex and ascii.
      // Prints the memory between and including the
      // two "end1" and "end2" pointers.
      void Show _Memory(const unsigned char * end1, const unsigned char * end2)
      {
          if(end2 >= end1)
          {
              Show _Memory(end1, end2 - end1 + 1);
          }
          else
          {
              Show _Memory(end2, end1 - end2 + 1);
          }
      }

      int main(int argc, char **args)
      {
          const char start [] = "hi there!  You're looking at me in memory!";
          const char * end = start + (int)strlen(start);

          Show _Memory((unsigned char *)start, (unsigned char *)end);

          return 0;
      }
(责任编辑:admin)



在程序的调试过程中,经常需要输出各种数据,正常情况下使用 printf 和 cout 即可实现数据输出。然而在输出二进制数据时, printf 和 out 却有点无能为力。那么如何比较二进制数据是否正确呢?

方案一:文件输出。文件可以输入任何数据,但是需要在程序之外比较文件,这对于少量数据并不划算。

方案二:实现自定义的十六进制输出函数。当然,也可是八进制,一般而言十六进制更易看懂 ( 习惯 ) 。下面给出一个最近实现的此类函数。该函数可将指定长度任何内存数据以十六进制格式输出。 这个程序对 32 和 64位的 PC 均适用。

注意: %x 无法正确打印负数,负数总是打印成 32bit 整型数, 64  PC 也是如此。


 

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <string>  
  3. void HexOutput(const char* buf, size_t len)  
  4. {  
  5.     printf("The Hex output of data :/n/t0x");  
  6.     for(size_t i=0; i<len; ++i)  
  7.     {  
  8.         unsigned char c = buf[i]; // must use unsigned char to print >128 value  
  9.         if( c< 16)  
  10.         {  
  11.             printf("0%x", c);  
  12.         }  
  13.         else  
  14.         {  
  15.             printf("%x", c);  
  16.         }  
  17.     }  
  18.     printf("/n");  
  19. }  
  20. int main()  
  21. {  
  22.     char c = 'A';  
  23.     HexOutput(&c, 1);  
  24.     c = 'a';  
  25.     HexOutput(&c, 1);  
  26.     c = 255;  
  27.     printf("/t%x/n", c);  
  28.     HexOutput(&c, 1);  
  29.     c = -1;  
  30.     HexOutput(&c, 1);  
  31.     printf("/t%x/n", c);  
  32.     short sc = -8;  
  33.     HexOutput((char*)&sc, 2);  
  34.     printf("/t%x/n", sc);  
  35.     char buf[20] = {0};  
  36.     HexOutput(buf, 20);  
  37.     std::string str = "BRSACP";  
  38.     HexOutput(str.c_str(), str.size());  
  39.     buf[0] = 0xFD; buf[1] = 0xFE;  
  40.     HexOutput(buf, 2);  
  41.     memcpy(buf+2, str.c_str(), str.size());  
  42.     HexOutput(buf, 20);  
  43.     long long value = 0xFDFE425253414350LLU;  // LLU or LL is necessary for 32 PC  
  44.     HexOutput((char*)&value, 8);  
  45.     Return 0;  
  46. }  

 

 


程序输出为:

The Hex output of data :  //char   c = 'A'

        0x41

The Hex output of data :  // char c=’a’

        0x61

        ffffffff

The Hex output of data :  // char c =255

        0xff

The Hex output of data :  // char c = -1

        0xff

        ffffffff

The Hex output of data :   // short sc = -8;

        0xf8ff

        fffffff8

The Hex output of data :

        0x0000000000000000000000000000000000000000

The Hex output of data :     // std::string str = "BRSACP";

        0x425253414350

The Hex output of data :     // buf[0] = 0xFD; buf[1] = 0xFE;

        0xfdfe

The Hex output of data :

        0xfdfe425253414350000000000000000000000000

The Hex output of data :

        0x504341535242fefd


参见:

内存打印函数 C++ 谷歌

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值