# convert number

I need a procedure .... for convert a decimal number to binary number, for example

3=00000011 ........convertir(3)
16= 00010000 .......convertir(16)
Asked:
###### Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x

Commented:
This what you're looking for.

Function BinString( Number : Byte ) : String;
Var
i : Integer;
begin
Result := '';
for i := 8 downto 0 do
begin
if ( Number AND ( 1 shl i )) <> 0 then
result := result + '1'
else
result := result + '0';
end;
end;

Isn't it?

Tchau
Reginaldo
0

Author Commented:
Edited text of question.
0

Commented:
function Int2binstring(value : cardinal) : string;
begin
if value = 0 then result := '0' else
begin
result := '';
while (value > 0) do
begin
if odd(value) then
result := '1' + result
else
result := '0' + result;
value := value shr 1;
end
end
end;
0

Commented:
Tchau, yes thats better but a waste of using a function, a proc could have done the same thing......

Function BinString( Number : Byte ) : String;
Var
i : Integer;
binstring2: string;
begin
binstring := '';
for i := 8 downto 0 do
begin
if ( Number AND ( 1 shl i )) <> 0 then
binstring2:=binstring2+'1'
else
binstring2 := binstring2 + '0';
end;
binstring:=binstring2;
end;

That Will Return it so then. stick an button and an edit box on... and type in this.

s:=binstring(10);
edit1.text:=s;

0

Commented:
Hi Craiq,

You were so unhappy in your comment that you can't even tell it.

1 - Tchau is not my name. That's a greeting. Means bye-bye;

2 - You suggest ( i didn't know why ) using a procedure but used a function as well;

3 - Why do you need declare a new variable ( binstring2 ) when Delphi declare it automatically?  ( Return is a declared variable you can use the same way you did with binstring2 and you don't need to assign this way  -  binstring := binstring2 -  anymore ).

4 - Do you know you could do this:
edit1.text:= BinString( AnyByte ) with that function I post here. Try it.

Tchau       ... Greeting
Reginaldo   ... my name

0

Commented:
Nope, Reginaldo, for your fuction could have easily have been a Procedure......

UnHappy? Unhappy about what?., I tried your code and in order for me to get it to work, i had to either a) declair another var either inside the proc, or ext in the var section, i thought the way i addedd to your code made more sence, try it.

Craig C.
0

Commented:
Moot point, isn't it guys?  I beat you by a full minute! :)
0

Author Commented:
thank
0

Commented:
yep u did.......
0

Commented:
But didn't get the points... :(
0

Commented:
and nor did anyone else :)
0

Commented:
oh wait some one did!!!
0

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.