1. New Thread Single items to number for Edit "Box" Solved
- Posted by Selgor Feb 06, 2011
- 3096 views
Hello
Selgor here
The following programme achieves the following goal
To get units of a sequence and make a "number"
Then enter the number in the appropriate "box"
It works for me using
eu 3.1 and my w32Lib
Thanks to all for reading the original post
and in particular to
Lnettnay , evanmars , WJ1N
Each gave me code to produce the final result
I worked the 3 lines to get the units as a single number
and the rest of the "trimmings"
Any constructive comments appreciated
Once again many thanks L , e , W
Cheers
Selgor
--------------------------------- Decimal to Binary ............ Version 1.12 -------------------------------- Many thanks to Lnettnay -------------------------------- also evanmars -------------------------------- also WJ1N -------------------------------------------------------------------------------- include Win32Lib.ew without warning global constant Window1 = create(Window,"",0,120,150,610,95,{WS_POPUP,WS_DLGFRAME}) setWindowBackColor(Window1,Parchment) -------------------------------------------------------------------------------- global constant EditText1 = createEx( EditText, "", Window1, 180, 10, 48, 20, 0, 0 ) , EditText2 = createEx( EditText, "", Window1, 180, 48, 148, 20, 0, 0 ) , LText1 = createEx( LText, "Type Decimal Number and Enter", Window1, 5, 10, 170, 20, 0, 0 ), LText2 = createEx( LText, "OR", Window1, 480, 40, 170, 20, 0, 0 ), LText5 = createEx( LText, "Left Click Window to exit", Window1, 400, 70, 192, 20, 0, 0 ), LText6 = createEx( LText, "Hit Enter for another No.", Window1, 400, 10, 192, 20, 0, 0 ), LText7 = createEx( LText, "Your Binary Number -------------->> ", Window1, 5, 50, 170, 20, 0, 0 ) setFont(LText2,"Times Roman",12,Italic+Bold) setFont(LText5,"Times Roman",12,Italic+Bold) setFont(LText6,"Times Roman",12,Italic+Bold) --------------------------------------------------------------------------------------------------- procedure dec_bin() sequence seq object number,n1,no,m,i seq = {} m = 1 no=0 i=0 number=getNumber(EditText1) n1 = number while n1 != 0 do seq = append(seq, remainder(n1,2)) n1 = floor(n1/2) i+=1 -----<----- 1 no = no + (seq[i]*m) -----<----- 2 m = m * 10 -----<----- 3 3 lines makes seq units a "number" end while setText(EditText2,sprintf("%d",no)) -----<<----**** Puts Binary Mumber in "Box" setVisible(LText2,1) setVisible(LText6,1) end procedure ---------------------------------------------------------------------------------- procedure Window1_onActivate (integer self, integer event, sequence params) --params is () setVisible(LText2,0) setVisible(LText6,0) setFocus(EditText1) end procedure setHandler( Window1, w32HActivate, routine_id("Window1_onActivate")) -------------------------------------------------------------------------------- procedure EditText1_onKeyDown (integer self, integer event, sequence params) --params is ( atom scanCode, atom shift ) if params[1]=13 then if self=EditText1 then doEvents(0) dec_bin() elsif self=EditText2 then setText(EditText1,"") setText(EditText2,"") setVisible(LText2,0) setVisible(LText6,0) end if end if end procedure setHandler( {EditText1,EditText2 }, w32HKeyDown, routine_id("EditText1_onKeyDown")) ------------------------------------------------------------------------------------------- procedure CloseApp ( integer self, integer event, sequence parms ) if self = Window1 then closeWindow(Window1) closeApp() end if end procedure setHandler(Window1, w32HClick, routine_id("CloseApp")) --------------------------------------------------------------------------------------------- WinMain( Window1,Normal )
2. Re: New Thread Single items to number for Edit "Box" Solved
- Posted by Lnettnay Feb 06, 2011
- 3010 views
Hi Selgor,
I'm sorry to tell you that you managed to get your conversion backwards again. You need to use
seq = prepend(seq, remainder(n1, 2)) -- or seq = remainder(n1, 2) & seq
Also the 3 lines to convert the sequence of bits to a decimal number are really unnecessary. The two lines
seq += 48 setText(EditText2, sprintf("%s", {seq})) --will convert the binary digits 0 and 1 to their ASCII eqivalent -- '0' and '1' and place them in the EditText box for you.
Lonny
3. Re: New Thread Single items to number for Edit "Box" Solved
- Posted by Selgor Feb 06, 2011
- 3011 views
Hello Lonny
I have attached your code below
If I enter 4 in decimal number
I get 1'a' instead of 10
and maybe 1|' instead of 10
So what is wrong ??
while n1 != 0 do seq = prepend(seq, remainder(n1,2)) n1 = floor(n1/2) seq += 48 setText(EditText2, sprintf("%s", {seq})) end while
Cheers
Selgor
4. Re: New Thread Single items to number for Edit "Box" Solved
- Posted by Selgor Feb 06, 2011
- 2988 views
Lonny
It is Selgor again
The zeroes are not showing as 0
but as "other" characters
Cheers
Selgor
5. Re: New Thread Single items to number for Edit "Box" Solved
- Posted by Lnettnay Feb 07, 2011
- 3116 views
Hello Lonny
I have attached your code below
If I enter 4 in decimal number
I get 1'a' instead of 10
and maybe 1|' instead of 10
So what is wrong ??
while n1 != 0 do seq = prepend(seq, remainder(n1,2)) n1 = floor(n1/2) end while -- move 2 lines below after the end while seq += 48 setText(EditText2, sprintf("%s", {seq}))
Cheers
Selgor
Hi Selgor,
Try moving the 2 lines as shown. What is happening is that you are adding the 48 to the 1st bit three times, to the 2nd bit twice and the 3rd bit once. If you move it below the 'end while' it will only be added once to each bit.
Lonny
6. Re: New Thread Single items to number for Edit "Box" Solved
- Posted by Selgor Feb 07, 2011
- 3103 views
Hello Lonny.
Well Derek Parnell answered my post about hex programme . It is in the posts. Get only a D. He has taken an entirely different approach to writing the programme. He uses a function. So Lonny we have a prog that works for Binary, Octal, Hexadecimal. While we think we are pretty good programmers, and you are much better than me,Derek is a brilliant programmer.
Thanks again for your help. Much appreciated. Hope to read you in the future. Oh, and by the way, my programme does work. It is in the conversion (prepend,append) stuff is all fixed up.
Cheers. Selgor.
constant hexdigit = "0123456789ABCDEF" function dec_hex(object number) sequence seq integer digit seq = "" while 1 do digit = remainder(number, 16) seq = hexdigit[digit + 1] & seq number = floor( number/ 16) if number = 0 then exit end if end while return seq end function