先晒个图吧。。

下面是程序代码:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    StaticText1: TStaticText; //这组件跟标签label效果差不多
    StaticText2: TStaticText;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    function BitToInt(str:string):Integer; //2进制转10进制函数声明
    function HextoBinary(Hexstr:string):string; //16进制转2进制函数声明
  private

    { Private declarations }
  public

    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function tform1.BitToInt(str:string):Integer;  //2进制转10进制
var
  i,size:Integer;
begin
  Result:=0;
  size:=Length(str);
  for i:=size downto 1 do
  begin
    if Copy(str,i,1)='1' then
    Result:=Result+(1 shl (size-i));
  end;
end;
function tform1.HextoBinary(Hexstr:string):string; //16进制转2进制
const
    BOX: array [0..15] of string =      //16进制的每1位数都对应一个4位的2进制数
         ('0000','0001','0010','0011',
          '0100','0101','0110','0111',
          '1000','1001','1010','1011',
          '1100','1101','1110','1111');
var
    i:integer;
begin
    for i:=Length(Hexstr) downto 1 do
        Result:=BOX[StrToInt('$'+Hexstr[i])]+Result;
end;
procedure TForm1.Button1Click(Sender: TObject); //2进制转10进制再转16进制
var
  a:Integer;
begin
  a:=BittoInt(Edit1.Text);  //先用2进制将(Edit1.Text)转换成10进制
  Edit2.Text:=IntToHex(a,1); //再用delphi自带的10进制转16进制函数(IntToHex)
end;
procedure TForm1.Button2Click(Sender: TObject);  //调用BitToInt
begin
Edit1.Text:=HextoBinary(Edit2.Text);
end;
end.
posted on 2010-10-26 17:18  巅枫  阅读(359)  评论(0编辑  收藏  举报