function T16210(k: string): integer; //十六进制字符串转换为十进制数
begin
result := StrtoInt('$' + k);
end;
function T1022(i:integer):string; //10进制转2进制
var
j: integer;
s: string;
begin
j := i;
s := ' ';
while j >= 2 do
begin
if (j mod 2) = 1 then
begin
s := '1' + s;
j := j div 2;
end
else
begin
s := '0' + s;
j := j div 2;
end;
end;
s := chr(ord('0') + j) + s;
result := s;
end;procedure TForm1.Button1Click(Sender: TObject);
var str:string;
begin
str:=inttostr(T16210(edit1.Text));
edit1.Text:= str;
end;输入小一点的数字比如 f,ff,1234,abc,都可以正常转化
可是我要转化的是比较大的数字.比如以下几组数字,
要转化的16进制数
8524B5C8
8524B5C7
8524B5C6
转化结果(明显就不对)
-2061236984
-2061236985
-2061236986
这是什么原因了,得用什么办法解决啊?
begin
result := StrtoInt('$' + k);
end;
function T1022(i:integer):string; //10进制转2进制
var
j: integer;
s: string;
begin
j := i;
s := ' ';
while j >= 2 do
begin
if (j mod 2) = 1 then
begin
s := '1' + s;
j := j div 2;
end
else
begin
s := '0' + s;
j := j div 2;
end;
end;
s := chr(ord('0') + j) + s;
result := s;
end;procedure TForm1.Button1Click(Sender: TObject);
var str:string;
begin
str:=inttostr(T16210(edit1.Text));
edit1.Text:= str;
end;输入小一点的数字比如 f,ff,1234,abc,都可以正常转化
可是我要转化的是比较大的数字.比如以下几组数字,
要转化的16进制数
8524B5C8
8524B5C7
8524B5C6
转化结果(明显就不对)
-2061236984
-2061236985
-2061236986
这是什么原因了,得用什么办法解决啊?
begin
result := StrtoInt64('$' + k);
end;
Integer -2147483648..2147483647 signed 32-bit
Int64 -2^63..2^63-1 signed 64-bit
但是10进制转化成2进制好像是又溢出了.procedure TForm1.Button5Click(Sender: TObject);
begin
edit5.Text:=T1022(strtoint64('$'+edit5.Text));
end;
测试18524B5C8结果
?(就显示一个?号)
----------------------------------
测试2
ffffffff结果也是?号按理说,自定义函数10进制转二进制,不是把他当作字符串处理了吗,.应该是那个j溢出了吧.int64也装不下有什么好办法解决吗?
你的函数,T1022参数是Integer,所以,这里就溢出了。
int64不是可以存储 -2^63..2^63-1 signed 64-bit按理说,足够存储的哈,
我改string也试过了,都溢出了.怎么回事了
你函数里定义的j:integer;
而j:=i;就已经溢出了,改成 j:int64;
var
j: int64;
s: string;
begin
j := i;
s := ' ';
while j >= 2 do
begin
if (j mod 2) = 1 then
begin
s := '1' + s;
j := j div 2;
end
else
begin
s := '0' + s;
j := j div 2;
end;
end;
s := chr(ord('0') + j) + s;
result := s;
end;
我把修改后的自定义函数帖一下吧函数返回值的类型也改成int64试过了,结果都一样啊.
begin
result := StrToInt64('$' + k);
end;
function T1022(i:Int64):string; //10进制转2进制
var
j: int64;
s: string;
begin
j := i;
s := ' ';
while j >= 2 do
begin
if (j mod 2) = 1 then
begin
s := '1' + s;
j := j div 2;
end
else
begin
s := '0' + s;
j := j div 2;
end;
end;
s := chr(ord('0') + j) + s;
result := s;
end;
你好,
改成longword之后不是还得转成string才能输出到edit框中.
IntToStr(Int64(...)).查了资料,说是这样转,试了不行呃.
const
HEX_BINSTR_Prefx: array [0..15] of String[4] = ( '0000'
, '0001'
, '0010'
, '0011'
, '0100'
, '0101'
, '0110'
, '0111'
, '1000'
, '1001'
, '1010'
, '1011'
, '1100'
, '1101'
, '1110'
, '1111');
HEX_BINSTR: array [0..15] of String[4] = ( ''
, '1'
, '10'
, '11'
, '100'
, '101'
, '110'
, '111'
, '1000'
, '1001'
, '1010'
, '1011'
, '1100'
, '1101'
, '1110'
, '1111');
var
sOut: String;
pIn,pOut: Pchar;
Index, I, iCount, iLen: Integer;
begin
Result := '';
if sIn = '' then Exit;
I := Length(sIn);
pIn := PChar(sIn);
iCount := I * 4;
case pIn^ of
'0':Dec(iCount, 4);
'1':Dec(iCount, 3);
'2','3':Dec(iCount, 2);
'4','5','6','7':Dec(iCount, 1);
'8','9','A'..'F','a'..'f':
else
Exit;
end;
SetLength(sOut, iCount);
pOut := PChar(sOut);
Index := 0;
case pIn^ of
'0':;
'1'..'9':Index := Ord(pIn^) - Ord('0');
'A'..'F':Index := Ord(pIn^) - Ord('A') + 10;
'a'..'f':Index := Ord(pIn^) - Ord('a') + 10;
end;
if Index <> 0 then begin
iLen := Length(HEX_BINSTR[Index]);
Move(PChar(HEX_BINSTR[Index])^, pOut^, iLen);
Inc(pOut, iLen);
end; Inc(pIn);
Dec(I); while I > 0 do begin
case pIn^ of
'0'..'9':Index := Ord(pIn^) - Ord('0');
'A'..'F':Index := Ord(pIn^) - Ord('A') + 10;
'a'..'f':Index := Ord(pIn^) - Ord('a') + 10
else
Exit;
end;
iLen := Length(HEX_BINSTR_Prefx [Index]);
Move(PChar(HEX_BINSTR_Prefx[Index])^, pOut^, iLen);
Inc(pOut, iLen);
Inc(pIn);
Dec(I);
end;
Result := sOut;
end;
输出 10000101001001001011010111001000