procedure ShowRawData( pPacket: PChar; Len: integer ); 
// shows raw data content, tabulated in hex & ASCII
const
  BytesPerLine = 16;
var
  i             : integer;
  Hbuf,
  AscBuf        : string;
begin
  HBuf := '';
  AscBuf := '';
  for i := 0 to pred( Len ) do
  begin
    if (i mod BytesPerLine = 0) and (HBuf <> '') then
    begin
      Log( HBuf + ' | ' + AscBuf , clPURPLE);
      HBuf := ''; AscBuf := '';
    end;
    // 16进制
    HBuf := HBuf + IntToHex( ord(( pPacket + i )^), 2)+' ';
    // ASCII码
    if ( pPacket + i )^ in [#32..#127] then
      AscBuf := AscBuf + ( pPacket + i )^
    else
      AscBuf := AscBuf + '.';
  end;
  // 最后一行
  if HBuf <> '' then
  begin
    while length(HBuf) < BytesPerLine*3 do
          HBuf := HBuf + '   ';
    while length(AscBuf) < BytesPerLine do
          AscBuf := AscBuf + ' ';
    Log( HBuf + ' | ' + AscBuf, clPURPLE );
  end;
  Log(' ', clPURPLE);end;
procedure Log( Txt: string; LogColor: TColor );
begin
   if length( reLog.Text ) > 100000 then
      reLog.Clear;
   with reLog.SelAttributes do
     if Color <> LogColor then
        Color := LogColor;
   reLog.Lines.Add( Txt );
end;
===========================
relog 为 TRichEdit

解决方案 »

  1.   

    老兄啊,你帮我解决的是16和asc转换的问题,我最主要的问题是client socket接收到数据时,会产生一个onread事件:
    procedure TForm1.ClientSocket1Read(Sender: TObject;  Socket: TCustomWinSocket);有两个函数可以接受传递过来的数据:
    Socket.ReceiveBuf(var buf: Untyped; count: integer):Integer
    Socket.ReceiveText: string我该用哪个呢?我开始先放在strtemp := Socket.ReceiveText;中,然后再读
    进mstemp: Tmemorystream中,然后每次读一个字节进行转换的。不过这样好像有问题,因为我是字符串读进去,字节读出来。数值不对。该怎么处理呀?各位大虾
      

  2.   

    我现在想知道的具体问题是:
    从网络上接收到的数据是什么格式的?二进制流?然后我要把得到的数据转换成ascii和hex.假设
    var
      str: string; 
      ms: Tmemorystream;
      chr: char;
      i: integer;
    begin
      ms := Tmemorystream.create;
      try
        str := '111';
        ms.write(str, length(str));
        ms.position := 0;
        for i := 1 to 3 do
        begin
          ms.read(chr, 1);
          showmessage(chr);
        end;
      finally 
        ms.free;
      end;
    end;
      
    得到的并不是我想要的三个'1',而是'D','#','E',请问这是为什么?谢谢大家
      

  3.   

    unit HexDump; 
    interfaceuses SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;const MAXDIGITS = 16;{ THexDump }typeTHexStr = array[0..2] of Char; THexStrArray = array[0..MAXDIGITS-1] of THexStr;THexDump = class(TCustomControl) private FActive: Boolean; FAddress: Pointer; FDataSize: Integer; FTopLine: Integer; FCurrentLine: Integer; FVisibleLines: Integer; FLineCount: Integer; FBytesPerLine: Integer; FItemHeight: Integer; FItemWidth: Integer; FFileColors: array[0..2] of TColor; FShowCharacters: Boolean; FShowAddress: Boolean; FBorder: TBorderStyle; FHexData: THexStrArray; FLineAddr: array[0..15] of char; FCharData: array[0..MAXDIGITS] of char;procedure CheckActive; procedure CalcPaintParams; procedure SetTopLine(Value: Integer); procedure SetCurrentLine(Value: Integer); procedure SetFileColor(Index: Integer; Value: TColor); function GetFileColor(Index: Integer): TColor; procedure SetShowCharacters(Value: Boolean); procedure SetShowAddress(Value: Boolean); procedure SetBorder(Value: TBorderStyle); procedure SetAddress(Value: Pointer); procedure SetDataSize(Value: Integer); procedure AdjustScrollBars; function LineAddr(Index: Integer): PChar; function LineData(Index: Integer): PChar; function LineChars(Index: Integer): PChar; function ScrollIntoView: Boolean; procedure InvalidateLine(Index: Integer); procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED; procedure CMEnter(var Message: TCMGotFocus); message CM_ENTER; procedure CMExit(var Message: TCMLostFocus); message CM_EXIT; procedure WMSize(var Message: TWMSize); message WM_SIZE; procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL; procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE; protected procedure CreateParams(var Params: TCreateParams); override; procedure Paint; override; procedure KeyDown(var Key: Word; Shift: TShiftState); override; procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property CurrentLine: Integer read FCurrentLine write SetCurrentLine; property Address: Pointer read FAddress write SetAddress; property DataSize: Integer read FDataSize write SetDataSize; published property Align; property Border: TBorderStyle read FBorder write SetBorder; property Color default