1. Sequence items to a single number ? How to achieve ?
- Posted by Selgor Feb 04, 2011
- 1385 views
Hello
Selgor here
I am working on decimal to binary conversion
I have Decimal number which is e.g. 5 and gives 101 in Binary [O.K. we all know this ]
Number 101 is stored in sequence as {1,0,1}
So seq[1]= 1 seq[2]= 0 and seq[3] = 1
how can I print each sequence item as 1 number ?
I thought that this would work :=
100x1 + 10x0 + 1x1 = 101
and it does
But can you imagine converting 13245 to binary and then writing number "my way"
There must be a simpler way
Cannot find a semblance in ref man
But as usual maybe I am not looking in the correct place
and using above
Any help appreciated
Cheers
Selgor
2. Re: Sequence items to a single number ? How to achieve ?
- Posted by Lnettnay Feb 04, 2011
- 1401 views
Here's one way to do it
sequence pbin, bin = {{1, 0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1}} pbin = bin + 48 for i = 1 to 2 do printf(1,"%s\n",{pbin[i]}) end for
3. Re: Sequence items to a single number ? How to achieve ?
- Posted by Selgor Feb 04, 2011
- 1367 views
Hello
Selgor here
I should have given you the code
Sorry
Where would you put your code in the following
I tried but no work
It is me NOT your code
Thank you for replying
Cheers
Anticipating your reply
programme:=
include get.e sequence seq seq = repeat(0,30) object number,n1,times,w number = 0 times = 0 n1 =0 number = prompt_number("Type a number then enter ",{}) n1 = number while n1 != 0 do times += 1 seq[times] = remainder(n1,2) n1 = floor(n1/2) end while puts(1,"\n") for i = 1 to times do puts(1,"Seq =") -- puts(1,"\n") printf(1,"%4d",seq[i]) puts(1,"\n") end for w= wait_key()
4. Re: Sequence items to a single number ? How to achieve ?
- Posted by evanmars Feb 04, 2011
- 1398 views
try this
include std/math.e integer d = 5 --your decimal number sequence b = {} while 1 do b = append(b,mod(d,2)) d = floor(d/2) if d = 0 then exit end if end while puts(1,"{") for i = 1 to length(b) do printf(1,"%d",b[i]) if i != length(b) then puts(1,",") end if end for puts(1,"}")
5. Re: Sequence items to a single number ? How to achieve ?
- Posted by Lnettnay Feb 04, 2011
- 1281 views
There's a small problem with your conversion routine (and also Selgor's). You are putting the bits into the array backwards. It doesn't show because 5 is palindromic in binary but if you try it with another number such as 13 you will see that the bits are reversed.
try this
include std/math.e integer d = 5 --your decimal number sequence b = {} while 1 do b = append(b,mod(d,2)) -- could either do -- b = prepend(b, mod(d,2)) -- or -- b = mod(d,2) & b d = floor(d/2) if d = 0 then exit end if end while puts(1,"{") for i = 1 to length(b) do printf(1,"%d",b[i]) if i != length(b) then puts(1,",") end if end for puts(1,"}")
6. Re: Sequence items to a single number ? How to achieve ?
- Posted by WJ1N Feb 04, 2011
- 1239 views
This is a somewhat different method
include std/convert.e integer d = 13 --your decimal number sequence b = {} b = reverse(int_to_bits(d)) b = b[find(1,b)..$] puts(1,"{") for i = 1 to length(b) do printf(1,"%d",b[i]) if i != length(b) then puts(1,",") end if end for puts(1,"}")
7. Re: Sequence items to a single number ? How to achieve ?
- Posted by Lnettnay Feb 04, 2011
- 1229 views
'reverse' is not a built-in. need to have 'include std/sequence.e'
8. Re: Sequence items to a single number ? How to achieve ?
- Posted by Lnettnay Feb 05, 2011
- 1209 views
Hello
Selgor here
I should have given you the code
Sorry
Where would you put your code in the following
I tried but no work
It is me NOT your code
Thank you for replying
Cheers
Anticipating your reply
programme:=
include get.e sequence seq = {} -- seq = repeat(0,30) don't need this line. let sequence grow as needed object number,n1,w --times number = 0 -- times = 0 n1 =0 number = prompt_number("Type a number then enter ",{}) n1 = number while n1 != 0 do -- times += 1 don't need -- seq[times] = remainder(n1,2) seq = prepend(seq, remainder(n1,2)) n1 = floor(n1/2) end while puts(1,"\n") for i = 1 to length(seq) do puts(1,"Seq =") printf(1,"%4d",seq[i]) puts(1,"\n") end for /*Now as to the formatting of the output\\ I thought you wanted to take a sequence such as {1, 0, 1, 1, 0, 1, 1} and print it out as '1011011' if so you would use seq += 48 printf(1,"%s\n",{seq}) */ w= wait_key()
Hi Selgor,
I cleaned up the formatting of the code and made a few changes. Check the comment block at the end and see if that is what you want.
9. Re: Sequence items to a single number ? How to achieve ?
- Posted by Selgor Feb 05, 2011
- 1227 views
Hello Lnettnay
My apologies for the delay in answering your last post
What you have done is exactly what I wanted e.g. {1,1,0,1} as 1101
I am NOT using Eu4 but 3.1
It still works
Gives the number
But with strange characters after number on 2 new lines
I will post again
A lot quicker this time
Hope you are still with me
And while it still works O.K.
Is there a way to save the number ??
Please read next post with programme and more details
It takes me a while to organise
Cheers
Selgor
10. Re: Sequence items to a single number ? How to achieve ?
- Posted by evanmars Feb 05, 2011
- 1135 views
Yes, my original program had prepend, but I wrote append in my post.
11. Re: Sequence items to a single number ? How to achieve ?
- Posted by Selgor Feb 05, 2011
- 1138 views
Hello all
Thank you for staying with me
The following is the programme
It works as written
I am using eu 3.1 not eu 4
I would like to place the binary number in the edit box of window
You will see all that I have tried
None works
So how does the binary number "go" into the "box"
Cheers
Selgor
--------------------------------- NewNarA1.exw ............ work No.1. include Win32Lib.ew without warning ------------------------------------------------------------------------------- sequence seq -------------------------------------------------------------------------------- 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, 48, 20, 0, 0 ) , LText1 = createEx( LText, "Type Number and Enter", Window1, 45, 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() integer number,n1 seq = {} number=getNumber(EditText1) n1 = number while n1 != 0 do seq = prepend(seq, remainder(n1,2)) n1 = floor(n1/2) end while for i = 1 to length(seq) do -- seq += 48 --setText(EditText2,sprintf("%s",seq[i])) --printf(1,"%s\n",{seq}) -- puts(1,"Seq =") -- seq += 48 -- setText(EditText2,{"%s"} seq[i]) printf(1,"%d",seq[i]) end for --?{seq} -- setText(EditText2,sprintf("%s %d",{seq})) --/*Now as to the formatting of the output\\ --I thought you wanted to take a sequence such as {1, 0, 1, 1, 0, 1, 1} and print it out as '1011011' if so you would use --seq += 48 --printf(1,"%s\n",{seq}) --*/ -- setText(EditText2,{seq}) 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 )