uses
Math;const
cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';function IntToDigit(mNumber: Integer; mScale: Byte; mLength: Integer = 0): string;
var
I, J: Integer;
begin
Result := '';
I := mNumber;
while (I >= mScale) and (mScale > 1) do begin
J := I mod mScale;
I := I div mScale;
Result := cScaleChar[J + 1] + Result;
end;
Result := cScaleChar[I + 1] + Result;
if mLength > 0 then
for I := 1 to mLength - Length(Result) do
Result := '0' + Result;
end; { IntToDigit }function DigitToInt(mDigit: string; mScale: Byte): Integer;
var
I: Byte;
L: Integer;
begin
Result := 0;
L := Length(mDigit);
for I := 1 to L do
Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) *
Trunc(IntPower(mScale, I - 1));
end; { DigitToInt }
Math;const
cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';function IntToDigit(mNumber: Integer; mScale: Byte; mLength: Integer = 0): string;
var
I, J: Integer;
begin
Result := '';
I := mNumber;
while (I >= mScale) and (mScale > 1) do begin
J := I mod mScale;
I := I div mScale;
Result := cScaleChar[J + 1] + Result;
end;
Result := cScaleChar[I + 1] + Result;
if mLength > 0 then
for I := 1 to mLength - Length(Result) do
Result := '0' + Result;
end; { IntToDigit }function DigitToInt(mDigit: string; mScale: Byte): Integer;
var
I: Byte;
L: Integer;
begin
Result := 0;
L := Length(mDigit);
for I := 1 to L do
Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) *
Trunc(IntPower(mScale, I - 1));
end; { DigitToInt }
var
I: Integer;
begin
I := DigitToInt(UpperCase(Edit1.Text), 16);
Edit2.Text := IntToDigit(I, 2);
end;
你真是一个高手
var
I: Integer;
begin
I := DigitToInt(UpperCase(Edit2.Text), 2);
Edit1.Text := IntToDigit(I, 16);
end;
cBinStrings: array[0..15] of string =
(
'0000', '0001', '0010', '0011',
'0100', '0101', '0110', '0111',
'1000', '1001', '1010', '1011',
'1100', '1101', '1110', '1111'
);function BinToHex(mBin: string): string;
var
I, J, L: Integer;
S: string;
begin
S := '';
L := Length(mBin);
if L mod 4 <> 0 then
for I := 1 to 4 - (L mod 4) do
mBin := '0' + mBin; for I := Length(mBin) downto 1 do begin
S := mBin[I] + S;
if Length(S) = 4 then begin
for J := 0 to 15 do
if S = cBinStrings[J] then begin
S := IntToHex(J, 1);
Break;
end;
if Length(S) > 1 then
Result := '0' + Result
else Result := S + Result;
S := '';
end ;
end;
end; { BinToHex }function HexToBin(mHex: string): string;
var
I: Integer;
begin
Result := '';
for I := 1 to Length(mHex) do
Result := Result + cBinStrings[StrToIntDef('$' + mHex[I], 0)];
while Pos('0', Result) = 1 do Delete(Result, 1, 1);
end; { HexToBin }procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := HexToBin(Edit2.Text);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Edit2.Text := BinToHex(Edit1.Text);
end;