[Gambas-user] binary numeral system

Emil Tchekov emil at ...1913...
Tue Apr 14 14:24:06 CEST 2009


Just checked the gambas online documentation.

The TextBox does have "CHANGE" event - thus it is possible to make converter
wich reacts on each additional letter typed

as example here my "fast shoot" maded in VB6 - very simple and without error
handling (BUT WORKING!)


Private Sub txtBin_Change()
    Dim StrBin As String

    StrBin = Me.txtBin

    If StrBin <> "" Then

        Me.lblDec.Caption = CStr(dec(StrBin))

    Else

        Me.lblDec.Caption = "n.a."

    End If

End Sub


Private Function dec(ByVal StrBin As String) As Long
    Dim i As Integer
    Dim l As Integer
    Dim c As String



    l = Len(StrBin)

    dec = 0

    For i = 1 To l

        c = Mid(StrBin, i, 1)

        If c = "1" Then
            dec = dec + 2 ^ (l - i)
        End If

    Next i

End Function



very best regards

Emil




-----Ursprüngliche Nachricht-----
Von: Simonart Dominique [mailto:simonart.dominique at ...11...]
Gesendet: Dienstag, 14. April 2009 14:04
An: mailing list for gambas users
Betreff: Re: [Gambas-user] binary numeral system


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
>
> sorry about my english but it has been long time since i had english
lessons...
> plz if u dont understand something let me know and i will try to explain
better...
> if you want to contact me directly :tsopanotragi at ...67... |
vlahonick at ...626... | chris at ...2135...
>
> thanks in advance Vlahonick (all the truth is always in
www.vlahonick.freehost.gr)
>
> _________________________________________________________________
> News, entertainment and everything you care about at Live.com. Get it now!
> http://www.live.com/getstarted.aspx
> --------------------------------------------------------------------------
----
> This SF.net email is sponsored by:
> High Quality Requirements in a Collaborative Environment.
> Download a free trial of Rational Requirements Composer Now!
> http://p.sf.net/sfu/www-ibm-com
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>

Hi Vlahonick,

I'm not sure to understand exactly what you want to do!
1) If you want to convert a bynary number to decimal value,
you just have to write in Gambas exactly what you sayd in
your message! :)
2) If you want to display the changing decimal value as soon
as you enter a binary digit, this is not possible because
you don't know what WILL be the final length of the binary
number!

For the 1) comprehension, here is an exemple (I took a
TextArea because I want to start a new line in the text)

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

Hope I understand you!
Dominique Simonart


----------------------------------------------------------------------------
--
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Gambas-user mailing list
Gambas-user at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user





More information about the User mailing list