您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

Delphi中查看内存数据的函数

时间:2011/9/3 15:31:35 点击:

  核心提示:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,...
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{用十六进制查看内存的函数; 参数1是内存起点, 参数2是以字节为单位的长度}
function ToHex(p: PByteArray; bit: Integer): string;
var
  i: Integer;
begin
  for i := 0 to bit - 1 do
    Result := IntToHex(p^[i], 2) + Chr(32) + Result;
  Result := TrimRight(Result);
end;

{用二进制查看内存的函数; 参数1是内存起点, 参数2是以字节为单位的长度}
function ToBin(p: PByteArray; bit: Integer): string;
const
  Convert: array['0'..'F'] of string = (
    '0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001',
    '', '', '', '', '', '', '', '1010', '1011', '1100', '1101', '1110', '1111');
var
  i: Integer;
  s: string;
begin
  s := ToHex(p, bit);
  for i := 1 to Length(s) do
    if s[i] <> Chr(32) then
      Result := Result + Convert[s[i]]
    else
      Result := Result + Chr(32);
end;

{测试一}
procedure TForm1.Button1Click(Sender: TObject);
var
  num: Integer;
begin
  Randomize;
  num := Random(MaxInt);
  ShowMessage(IntToStr(num) + #10#13#10#13 +
              ToHex(@num, 4) + #10#13#10#13 +
              ToBin(@num, 4));
end;

{测试二}
procedure TForm1.Button2Click(Sender: TObject);
var
  str: string;
begin
  str := 'Delphi 2010';
  ShowMessage(str + #10#13#10#13 +
              ToHex(@str[1], Length(str)*SizeOf(str[1])) + #10#13#10#13 +
              ToBin(@str[1], Length(str)*SizeOf(str[1])));
end;

end.

作者:万一 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号