求助:整形怎么转换二进制 ( 积分: 100 )

  • 主题发起人 big-small
  • 开始时间
B

big-small

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:
大家帮帮忙,整形怎么转换成二进制,请写一下代码
最好定义一个函数
 
各位高手:
大家帮帮忙,整形怎么转换成二进制,请写一下代码
最好定义一个函数
 
查帮助 :IntToBin
 
function TForm1.IntToBinStr(n: integer): string;
var
S: string;
i: integer;
b: boolean;
begin
if n < 0 then
b := true
else
b := False;
n := Abs(n);
for i := 1 to SizeOf(n) * 8do
begin
if n < 0 then
S := S + '1'
else
S := S + '0';
n := n shl 1;
end;
Delete(S,1,Pos('1',S) - 1);//remove leading zeros
if b then
S := '-' + S;
Result := S;
end;
 
一个整形可以转换为4个byte
IntToByte(var A: array of Byte int:integer)
A是 保存转换后的4 个BYTE,int是要转换的整数
begin
A[0]:= Byte(int and $FF);
A[1]:= Byte((int and $FF00) shr 8);
A[2]:= Byte((int and $FF0000) shr 16);
A[3]:= Byte((int and $FF000000) shr 24);
end;
procedure ByteToInt(A:array of Byte;var int:integer);
begin
int:=0;
int:= int or A[0];
int:= int or (A[1] shl 8);
int:= int or (A[2] shl 16);
int:= int or (A[3] shl 24);
end;
 
//value是为要转换的十进制数
function inttobin(value: integer): string;
begin
result:='';
while value<>0do
begin
result:=inttostr(value mod 2)+result;
value:=value div 2
end;
end;
 
//AiValue 待转换的整数(10进制)
//AiKey 进制 (2表示二进制/8表示八进制/最多36进制,默认2进制)
function IntConvertEX(AiValue: Integer;
Const AiKey : Integer = 2): String;
CONST
BASECHAR = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Var
i : Integer;
begin
Result := '';
If (AiKey>36) Or (AiKey<2) Or (AiValue<=0) then
begin
Result := '0';
Exit;
end;
i := AiValue;
While i>0do
begin
Result := BASECHAR[(i Mod AiKey)+1] + Result;
i := i Div AiKey;
end;
end;
 
THzSpell.PyHeadOfHz组件有没有
 
//递归法
//AiValue 待转换的整数(10进制)
//AiKey 进制 (2表示二进制/8表示八进制/最多36进制,默认2进制)
function IntConvertEX(AiValue: Integer;
Const AiKey : Integer = 2): String;
CONST
BASECHAR = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Var
i : Integer;
begin
Result := '';
If (AiKey>36) Or (AiKey<2) Or (AiValue<0) then
Exit;
i := AiValue;
If i<AiKey then
Result := BASECHAR[i+1] + Result
else
Result := IntConvertEX((i Div AiKey), AiKey) + BASECHAR[(i Mod AiKey)+1];
end;

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
//Edit1.Text := IntConvertEX(17, 2);
Edit1.Text := IntConvertEX(StrToInt(Edit2.Text), StrToInt(Edit3.Text));
end;
 
function HexBitToBin(ch: char): string;
begin
case ch of
'1': result := '0001';
'2': result := '0010';
'3': result := '0011';
'4': result := '0100';
'5': result := '0101';
'6': result := '0110';
'7': result := '0111';
'8': result := '1000';
'9': result := '1001';
'A': result := '1010';
'B': result := '1011';
'C': result := '1100';
'D': result := '1101';
'E': result := '1110';
'F': result := '1111';
else
result := '0000';
end;
end;

function IntToBin(n: integer): string;
var
i, index: integer;
hex: string;
begin
hex := IntToHex(n, 8);
result := '';
for i := 1 to 8do
result := result+HexBitToBin(hex);
if (result[1] = '0') then
begin
index := pos('1', result);
if index > 0 then
delete(result, 1, index-1)
else
result := '0';
end;
end;
虽然有HexToBin这个函数但我不会用,还要请大侠教我
 

Similar threads

回复
0
查看
670
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
回复
0
查看
631
爱音乐的孩子是小白
顶部