[Gambas-user] binary numeral system
Ron_1st
ronstk at ...239...
Tue Apr 14 18:28:20 CEST 2009
On Tuesday 14 April 2009, Simonart Dominique wrote:
> PUBLIC SUB Button1_Click()
> DIM S AS String
> DIM i, y, N AS Integer
>
> S = TextArea1.Text
> y = Len(S)
> if y = 0 THEN RETURN
> N = 0
> FOR i = 1 to y
> d = Val(Mid(S, i, 1)) ' I assume that d is 0 or 1
> N += d * (2 ^ (y-i))
> NEXT
> TextArea1.Text &= "\nBinary " & S & " = decimal " & Str(N)
> END
>
function bin2dec(sBin) as long
dim r as long
r=0
for i= 1 to len(sBin)
r = r*2 + Val(Mid(sBin, i, 1))
next
return r
end
> PUBLIC SUB Button1_Click()
> TextArea1.Text = "\nBinary " & TextArea1.Text & " = decimal " & bin2dec(TextArea1.Text)
> END
for 10011 as sBin the sequence is as follows:
r*2=0 +1 = 1
r*2=2 +0 = 2
r*2=4 +0 = 4
r*2=8 +1 = 9
r*2=18 +1 =19
I does not matter the size of input string.
The only limit is r should of apropiate type
to hold the result.
Multiply is faster as Power
Best regards,
Ron_1st
More information about the User
mailing list