我正在编写一个基本转换器,因为我很快就会进行测试,并且我需要将二进制数转换为 3 个不同的基数:八进制、十进制和十六进制。我已经编写了将二进制字符串转换为十进制和十六进制的代码。
function bintodec(Value:string;dec:TEdit;hexadec:TEdit): Integer;
var //dec and hexadec are the TEdits where I will put the result
i, iValueSize: Integer;
Edit2,f:TEdit;
begin
Result := 0;
iValueSize := Length(Value);
for i := iValueSize downto 1 do
begin
if Value[i] = '1' then Result := Result + (1 shl (iValueSize - i));
end;
dec.Text:=(IntToStr(Result)); //dec. number
hexadec.Text:=(IntToHex(Result,8)); //hexadec. number
end;
正如您在此处看到的,该函数采用一个字符串(例如 10101001)并将结果放入 2 个不同的编辑中。
我制作了一个将十进制数转换为八进制数的函数,但是当我按下 SpeedButton 时Calc.
出现错误。它说 project1 引发了一个类异常“外部:SIGSEGV”,然后在 Unit1 附近我看到了页面 control.inc。我在谷歌上搜索了一个解决方案,但没有找到有用的答案。
function dec2oct(mystring:Integer): String;
var
a: String;
getal_met_rest : Double;
Edit2:TEdit;
begin
while mystring> 0 do
begin
getal_met_rest := getal / 8;
a:= a + IntToStr(mystring - (trunc(getal_met_rest)*8));
getal := trunc(getal_met_rest);
end;
dec2oct:=ReverseString(a);
Edit2.text:=dec2oct
end;
我没有找到二进制八进制转换的方法,所以一旦我从二进制转换为十进制,我就调用函数dec2oct
. 我以这种方式调用函数:
var a:smallint;
begin
bintodec(Edit1.Text,Edit3,Edit4);
dec2oct(Edit3.Text); //Edit3 contains the number on base 10
end;
你可以帮帮我吗?