[Gambas-user] binary numeral system

Simonart Dominique simonart.dominique at ...11...
Thu Apr 16 02:05:00 CEST 2009


vlahonick vlahonick a écrit :
> 
> hello all :D
> 
> i am trying to create a program that calculates a number from the binary numeral system and gives a result
> 
> example of what the program will do :
> 
> we have the number (at binary numeral system) 10011
> 
> so to have result we have to do (1*2^4)+(0*2^3)+(0*2^2)+(1*2^1)+(1*2^0)=
> 16+0+0+2+1=19
> 
> to do that in a program first i have to calculate the powers
> so i have to write (y=number of digits) for the first of the digits y-1,second y-2etc
> 
> so if i have 5 digits then the power for the first one will be y-1=5-1=4
> 
> this is done by the Len(textbox1.text)
> 
> but HOW i will write Len(textbox1.text)=y ?????
> 
> then how i can make the program to recognize that for the 1st digit entered in the Textbox1.text have to
> give (d*2^y-1) (d=the digit that can be 0 or 1 and y the number of the digits) for the second (d*2^y-2)etc...
> 
> so the main question is how i can make JUST ONE TEXTBOX to understand that for every digits enter it will give something else for a result

As an exercise for myself, I modify the program so the 
decimal value is displayed for each binary digit you typed. 
Now, you don't need to click a button to know the value.
I could say it was not simple to understand how works this 
TextArea update with the cursor position

STATIC PUBLIC DecVal AS Integer
STATIC PUBLIC InputChain AS String

PUBLIC SUB Form_Open()
    Button1_Click()
END

PUBLIC SUB Button1_Click() ' CLEAR button
    DecVal = 0
    InputChain = ""
    TextArea1.Clear
END

PUBLIC SUB TextArea1_KeyPress()
DIM K as String

    K = Key.Text
    IF K <> "0" AND K <> "1" THEN RETURN
    DecVal *= 2
    DecVal += Val(K)
    TextArea1.Text = InputChain & "\nBinary " & InputChain & 
K & " = decimal " & Str(DecVal)
    TextArea1.Pos = Len(InputChain)
    InputChain &= K
END

Look how I have to separate InputChain and K to initialize 
the Text property. You must add the K character to the 
InputChain *after* the modification of the TextArea.

Hope this help
Dominique Simonart






More information about the User mailing list