近々テストがあり、2 進数を 3 つの異なる基数 (8 進数、10 進数、16 進数) に変換する必要があるため、基数コンバーターを作成しています。バイナリ文字列を 10 進数と 16 進数に変換するコードは既に作成しています。
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 つの異なる編集に入れます。
10 進数を 8 進数に変換する関数を作成しましたが、SpeedButtonCalc.
を押すとエラーが発生します。project1 がクラス例外 'External: SIGSEGV' を発生させ、Unit1 の近くに control.inc ページが表示されます。Google で解決策を検索しましたが、有用な回答が見つかりませんでした。
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;
2 進数から 8 進数への変換方法が見つからなかったので、2 進数から 10 進数に変換したら、関数 を呼び出しますdec2oct
。この方法で関数を呼び出します。
var a:smallint;
begin
bintodec(Edit1.Text,Edit3,Edit4);
dec2oct(Edit3.Text); //Edit3 contains the number on base 10
end;
私たちを手伝ってくれますか?