可能是你的二进制转换有问题。
-------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
Button2: TButton;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject;
var Key: Char);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//求一个字节的补码
function imp(i: byte):byte;
begin
if i<128 then
result := i else
result := not(i)+1;
end;
//二进制字符串转化为byte
function BinStrToDec(s: string): byte;
var
i, len: integer;
begin
result := 0;
len := length(s);
for i:=1 to lendo
begin
result := result + strtoint(s) * ((2 shl (len-i))shr 1);
end;
end;
//将一个字节以二进制字符串表示
function ByteToBinStr(b: byte): string;
var
m: byte;
begin
result := '';
repeat
m := b mod 2;
result := inttostr(m) + result;
b := b div 2;
until b<2;
result := inttostr(b)+result;
if length(result)<8 then
for m:=1 to (8-length(result))do
result := '0'+result;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := inttostr(BinStrToDec(Edit1.Text));
caption := format('%d',[imp(BinStrToDec(Edit1.Text))]);
end;
//在Edit1内输入'0'或'1'
procedure TForm1.Edit1KeyPress(Sender: TObject;
var Key: Char);
begin
if (not (Key in ['0','1',#8])) then
key := #0;
if (length(Edit1.Text)>=8)and(Key<>#8) then
key := #0;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Label2.Caption := ByteToBinStr(imp(BinStrToDec(Edit1.Text)));
end;
end.