如何把二进制转为十六进制?

backlove 2002-01-14 02:23:54
help
...全文
137 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2002-01-14
  • 打赏
  • 举报
回复
const
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;
backlove 2002-01-14
  • 打赏
  • 举报
回复
谢谢了,提个建议
数太大的时候会出错
backlove 2002-01-14
  • 打赏
  • 举报
回复
不好意思,怎么突然多了这么多回复,我试一下先
backlove 2002-01-14
  • 打赏
  • 举报
回复
不好意思,你的程序一行注释没有实在不好懂
好象不是二进制到16进制的吧,可能是我笨笨
王集鹄 2002-01-14
  • 打赏
  • 举报
回复
procedure TForm1.Button2Click(Sender: TObject);
var
I: Integer;
begin
I := DigitToInt(UpperCase(Edit2.Text), 2);
Edit1.Text := IntToDigit(I, 16);
end;
kylion 2002-01-14
  • 打赏
  • 举报
回复
format函数多简单!
xjl1980_81 2002-01-14
  • 打赏
  • 举报
回复
To zswang(伴水)(需要充充电):
你真是一个高手
王集鹄 2002-01-14
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
I := DigitToInt(UpperCase(Edit1.Text), 16);
Edit2.Text := IntToDigit(I, 2);
end;
王集鹄 2002-01-14
  • 打赏
  • 举报
回复
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 }

xjl1980_81 2002-01-14
  • 打赏
  • 举报
回复
那不是很简单吗?四位一组合即可,从右开始!

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧