程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 一個查看內存數據的Delphi函數

一個查看內存數據的Delphi函數

編輯:Delphi

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.

重寫:

function ToBin2(p: PByteArray; b: Integer): string;
var
i,j: Integer;
begin
Result := StringOfChar('0', b * 8);
for i := 0 to b - 1 do for j := 0 to 7 do
if Odd(p^[b-1-i] shr j) then Result[i*8 + 8 - j] := '1';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
f: Single;
str: string;
begin
f := -31.4;
str := ToBin2(@f, SizeOf(f));
Memo1.Lines.Add(str);
end;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved