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 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 -- Stay on top of everything new and different, both inside and around Java (TM) technology - register by April 22, and save $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco. 300 plus technical and hands-on sessions. Register today. Use priority code J9JMT32. http://p.sf.net/sfu/p ___ Gambas-user mailing list Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
[Gambas-user] binary numeral system
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 :tsopanotr...@hotmail.com | vlahon...@gmail.com | ch...@vlahonick.freehost.gr 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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
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 :tsopanotr...@hotmail.com | vlahon...@gmail.com | ch...@vlahonick.freehost.gr 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@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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
Re: [Gambas-user] binary numeral system
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.domini...@wanadoo.fr] 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 :tsopanotr...@hotmail.com | vlahon...@gmail.com | ch...@vlahonick.freehost.gr 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@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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user -- 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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
Re: [Gambas-user] binary numeral system
Gah - you just beat me :-) Mine is as follows. Interesting to see different styles/solutions: PRIVATE FUNCTION myBin(binString AS String) AS Long DIM i AS Integer DIM length AS Integer DIM binResult AS Long = 0 length = Len(binString) FOR i = length TO 1 STEP -1 binResult = binResult + CInt(Mid(binString, i, 1)) * Exp2(length - i) NEXT RETURN binResult END On Tue, 2009-04-14 at 14:24 +0200, Emil Tchekov wrote: 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.domini...@wanadoo.fr] 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 :tsopanotr...@hotmail.com | vlahon...@gmail.com | ch...@vlahonick.freehost.gr 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@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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
Re: [Gambas-user] binary numeral system
Ron_1st a écrit : 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 Thanks Ron! You're right, there is no need to use the power factor at all! My mind was not opened enough to see that! -- 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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
Re: [Gambas-user] binary numeral system
That's pretty cool. My only comment would be that it took me a few goes at stepping through the code before I worked out how the blazes it worked, so perhaps less readable than the exponent version. But hey - I'm no rocket scientist :-) From: ron...@tiscali.nl 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 _ Need a new place to rent, share or buy? Let ninemsn property search for you. http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn%2Edomain%2Ecom%2Eau%2F%3Fs%5Fcid%3DFDMedia%3ANineMSN%5FHotmail%5FTagline_t=774152450_r=Domain_tagline_m=EXT -- 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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user