记录log中的16进制和ASCII码字符输出

写通信程序常常需要把发出和收到的包记录到log文件,而最常用的记录方式就是16进制和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: }

输出结果样例:

   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   .... .... .... .... 
原文地址:https://www.cnblogs.com/Mainz/p/1148238.html