I'm writing a base converter because I have a test soon and I need to convert a binary number in 3 different bases: octal, decimal and hexadecimal. I've already written the code that convert a binary string into decimal and hexadecimal.
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;
As you can see here, the function takes a string (for example 10101001) and puts into 2 different edit the result.
I've made a function that convert a decimal number into an octal number but when I press the SpeedButton Calc.
I have an error. It says that project1 raised a class exception 'External: SIGSEGV' and then near to Unit1 I see the page control.inc. I've searched on google a solution but I didn't find useful answers.
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;
I didn't find a way for binary-octal conversion, so once I've converted from binary to decimal, I call the function dec2oct
. I call the functions in this way:
var a:smallint;
begin
bintodec(Edit1.Text,Edit3,Edit4);
dec2oct(Edit3.Text); //Edit3 contains the number on base 10
end;
Could you help me?
I would usually use string or character arrays and bit arithmetic for such conversions. For instance:
Seems to work in the short little bit of time that I tested it as long as you don't expect it to handle negative numbers. Edit: It handles zero now.
For other bases answer of Ken White is very usefull.
But in the unit StrUtils such functions already exists:
Here's a function that works somewhat like the C runtime library's
itoa
function, converting a positive integer value (Cardinal
in Delphi) to a specified radix between 2 and 36. It's been tested under Delphi 2007 and XE4.Just for fun, I decided to write a function to "undo" the conversion (convert from another radix back to decimal (base 10)). It's also modeled (very loosely) after the C RTL function
atoi
, except it requires you to pass in the radix of the number being passed.Note that
ConvertIntToBase
was tested with many numeric inputs, but I can only validate those bases supported by Windows Calculator in programmer mode (binary (base 2), octal (base 8), decimal (base 10, which I did not test), and hex (base 16)), as I don't have a calculator that will support other radix values and didn't want to do the work by hand. ;-)ConvertBaseToInt
was tested by passing in the test values ofConvertIntToBase
and confirming that what went into one was what came back out of the other; IOW, that a number converted into binary byConvertIntToBase
would result in the same number when run back throughConvertBaseToInt
.You can test it with something similar to this in a console application: