五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > 記錄log中的16進(jìn)制和ASCII碼字符輸出

記錄log中的16進(jìn)制和ASCII碼字符輸出

文章來源:365jz.com     點(diǎn)擊數(shù):727    更新時(shí)間:2009-09-19 10:43   參與評論

寫通信程序常常需要把發(fā)出和收到的包記錄到log文件,而最常用的記錄方式就是16進(jìn)制和ASCII碼左右對照的方式,

C代碼  :

   1: short DebugASCII( char *buf1, int leng, int line)
   2: {
   3:    int i, j=0;
   4:    char s[100], temp[5];
   5:  
   6:    printf( "Debug Information from Line: %04d\n", line);
   7:  
   8:    for( i=0; i<leng; i++)
   9:    {
  10:       if ( j==0)
  11:       {
  12:          memset( s, ' ', 84);
  13:          sprintf(temp,   " %03d:",i );
  14:          memcpy( s, temp, 5);
  15:          sprintf(temp,   ":%03d",i+15 );
  16:          memcpy( &s[72], temp, 4);
  17:       }
  18:       sprintf( temp, "%02X ", (unsigned char)buf1[i]);
  19:       memcpy( &s[j*3+5+(j>7)], temp, 3);
  20:       if ( isprint( buf1[i]))
  21:       {
  22:          s[j+55+(j>7)]=buf1[i];
  23:       }
  24:       else
  25:       {
  26:          s[j+55+(j>7)]='.';
  27:       }
  28:       j++;
  29:       if ( j==16)
  30:       {
  31:          s[76]=0;
  32:          printf( "%s\n", s);
  33:          j=0;
  34:       }
  35:    }
  36:    if ( j)
  37:    {
  38:       s[76]=0;
  39:       printf( "%s\n", s);
  40:    }
  41: } 

C#代碼:

   1: string ToPrintLog = LogHex ( 16, 8, message );
   2:  
   3: public static string LogHex ( int lineWidth, int spaceIndent, byte[] data )
   4: {
   5:         if ( data == null ) return null;
   6:  
   7:         string temp;
   8:         string line = String.Empty;
   9:  
  10:         StringBuilder sb = new StringBuilder ();
  11:         int j = 0;
  12:  
  13:         for ( int i = 0; i < data.Length; i++ )
  14:         {
  15:             temp = data[i].ToString ( "X2" ).PadLeft ( 2, '0' ); //0xce -> "CE"
  16:  
  17:             if ( j == 0 )
  18:                 line = temp;
  19:             else
  20:             {
  21:                 if ( (j+1) % spaceIndent == 0 )
  22:                     line = line.Insert ( ( j >= spaceIndent ) ? 2 * j + 1 : ( 2 * j ), temp+" " );
  23:                 else
  24:                     line = line.Insert ( ( j >= spaceIndent ) ? 2 * j + 1 : ( 2 * j ), temp );
  25:             }
  26:  
  27:             if ( data[i] >= 0x30 && data[i] <= 0x7E )
  28:             {
  29:                 temp=( (char)data[i] ).ToString ();
  30:             }
  31:             else
  32:             {
  33:                 temp= "." ;//invisible
  34:             }
  35:  
  36:             line = line.Insert ( line.Length, temp );
  37:  
  38:             j++;
  39:  
  40:             if ( j == lineWidth )
  41:             {
  42:                 sb.AppendLine ( line );
  43:  
  44:                 j = 0;
  45:  
  46:                 line = String.Empty;
  47:             }
  48:             else
  49:             {
  50:                 if ( i == data.Length - 1 )
  51:                 {
  52:                     int comCount = 1;
  53:  
  54:                     if ( j >= spaceIndent )
  55:                         comCount = 2 * lineWidth - 2 * j+1;
  56:                     else
  57:                         comCount = 2 * lineWidth - 2 * j +2;
  58:  
  59:                     for ( int m = 0; m < comCount; m++ )
  60:                     {
  61:                         line = line.Insert ( ( j >= spaceIndent ) ? 2 * j +1 : ( 2 * j ), " " );
  62:                     }
  63:                     sb.AppendLine ( line );
  64:                 }
  65:             }
  66:         }
  67:         return sb.ToString();
  68: }

 

輸出結(jié)果樣例:

   1: 14A654A 20476459 14A654A 20476459   .JeJ .GdY .JeJ .GdY 
   2: 6646A49 20202020 6646A49 20202020   edjI .... edjI .... 
   3: 21F4A65 4A204764 21F4A65 4A204764   ..Je J.Gd ..Je J.Gd 
   4: 565646A 49203130 565646A 49203130   Yedj I.10 Yedj I.10 
   5: 2202020 20202020 2202020 20202020   .... .... .... .... 
返回 返回

如對本文有疑問,請?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會為你解答??! 點(diǎn)擊進(jìn)入論壇

發(fā)表評論 (727人查看0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號