1. RE: Binary conversion
- Posted by rudy toews <rltoews at ilos.net> Aug 15, 2002
- 457 views
Jonas Temple wrote: > Okay, I asked this a while back and now I've got a slightly different > problem. > > If I have a sequence of {0,0,0,2} that is supposed to represent a binary > > number, how do I convert that to an atom? > > Jonas > > hi Jonas i did not see the orginal posting. but would a table be helpful? since the smallest element in Euphoria is an atom then a table of 32 entrys of the 32 different bit positions would be needed. using 'or', 'and' or 'xor' should work receiver = receiver or'd with table entry(input position) this is my first thought on it. working nights makes one groggy. later rudy lotterywars
2. RE: Binary conversion
- Posted by rswiston at hotmail.com Aug 15, 2002
- 428 views
One way might be to do this: atom binary, sum binary = {0,0,0,2} sum = 0 for x = 1 to length(binary) do sum += (power(10, length(binary)-x))*binary[x] end for this would convert any langth of binary into a decimal number (replace ten within the loop to convert to a different base) so 0,0,0,2 = 0*10^3 + 0*10^2 + 0*10^1 + 2*10^0 = 2 additionally.. 1,2,3,4,5,6 = 1*10^5 + 2*10^4 + 3*10^3 + 4*10^2 + 5*10^1 + 6*10^0 = 12345 Note: This will only change a sequence of "digits" into one atom (or number), not convert from one base into another. >From: rudy toews <rltoews at ilos.net> >Reply-To: EUforum at topica.com >To: EUforum <EUforum at topica.com> >Subject: RE: Binary conversion >Date: Thu, 15 Aug 2002 22:41:17 +0000 > > >Jonas Temple wrote: > > Okay, I asked this a while back and now I've got a slightly different > > problem. > > > > If I have a sequence of {0,0,0,2} that is supposed to represent a binary > > > > number, how do I convert that to an atom? > > > > Jonas > > > > >hi Jonas >i did not see the orginal posting. >but would a table be helpful? >since the smallest element in Euphoria is an atom then a table of 32 >entrys of the 32 different bit positions would be needed. > >using 'or', 'and' or 'xor' should work > >receiver = receiver or'd with table entry(input position) > >this is my first thought on it. working nights makes one groggy. >later >rudy > > >lotterywars > > > >
3. RE: Binary conversion
- Posted by Jonas Temple <jktemple at yhti.net> Aug 15, 2002
- 456 views
I really wrote the last post in a moment of frustration. What I didn't quite have a grasp on is the fact that if you do a poke4 of an atom with a value of 90 to memory and then do a peek({mem,4}) at the same location you get {90,0,0,0}. The PC expert would say, "Well, duh, a PC stores binary numbers with significant digit first!" (or last...no first...oh, whatever). What I've been working on is wrappers for DLLs that interface to an IBM iSeries. IBM's method is to store significant digits last so your 90 would look like {0,0,0,90}. I tried doing a value() against my {0,0,0,90} which returned nothing (as it should). So that's why I wondered about binary conversion. Still wondering on how to turn {0,0,0,90} back into 90. I'm gonna keep diggin'. Jonas
4. RE: Binary conversion
- Posted by Elliott Sales de Andrade <quantum_analyst at hotmail.com> Aug 15, 2002
- 435 views
>So that's why I wondered about binary conversion. Still wondering on >how to turn {0,0,0,90} back into 90. I'm gonna keep diggin'. Did you try this: include machine.e include misc.e constant binary = {0, 0, 0, 90} sequence rev integer answer -- reverse bytes so they will be 386 order rev = reverse(binary) -- convert to an integer answer = bytes_to_int(rev)
5. RE: Binary conversion
- Posted by Jonas Temple <jktemple at yhti.net> Aug 16, 2002
- 454 views
What a great community this is! I really appreciate everyone's willingness to help. Here's a little more background to my problem and the solution I came up with. I have an atom with a value of 90. If you were to poke4() this atom to memory and then peek4() you would get {90,0,0,0}. Since the iSeries expects the reverse order I called a translation routine to reverse the order before passing to the iSeries. Everything worked up to that point. When the data was returned to my PC is was {0,0,0,91} (remembering that I reversed it and the program on the iSeries added 1 to the number). I liked the reverse() and bytes_to_int() solution and it worked, but not on negative values (the doc states that bytes_to_int expects the sequence to contain positive values). I thought about Derek's multiplication approach but here's what I came up with: atom bin, parm sequence rtn_seq ...some stuff in here... bin = allocate(4) rtn_seq = reverse(rtn_seq) poke(bin, rtn_seq) parm = peek4s(bin) free(bin) So what I accomplished was I used reverse() to put the sequence back into the order needed for an atom, poked the sequence to memory and then used peek4s to get the value back out. Maybe not the most elegant solution but it worked nonetheless. Thanks again to everyone!
6. RE: Binary conversion
- Posted by Don Phillips <Graebel at hotmail.com> Aug 16, 2002
- 487 views
> Okay, I asked this a while back and now I've got a slightly different > problem. > > If I have a sequence of {0,0,0,2} that is supposed to represent a binary > > number, how do I convert that to an atom? > > Jonas Hey Jonas, I saw the other posts, but I am not real sure about what exactly it is you are trying to do. poke4 is a 32 bit command so you will store 4 bytes of information at a specified location. If this is the case, I do not see why you are using peek to get the information back out. Peek is an 8 bit command so, as posted, you would get back a 4 length sequence representing the 4 bytes that you wrote out earlier. Why not use peek4u or peek4s to read the data back out as these are the 32 bit routines for accessing memory? Write 90: poke4( mem, 90 ) Read 90: peek4u( mem ) Don
7. RE: Binary conversion
- Posted by Jonas Temple <jktemple at yhti.net> Aug 16, 2002
- 435 views
Don, The problem was that the IBM iSeries stores 32 bit values in the opposite order of the PC. What I wound up with after a call to a program on the iSeries is 4 bytes returned to me in the opposite order. You can't use peek4s or peek4u on the memory since peek4s/u is expecting the bytes to be in the PC order. Make sense? Don Phillips wrote: > Hey Jonas, > I saw the other posts, but I am not real sure about what exactly > it is you are trying to do. > > poke4 is a 32 bit command so you will store 4 bytes of information > at a specified location. If this is the case, I do not see why > you are using peek to get the information back out. Peek is an 8 > bit command so, as posted, you would get back a 4 length sequence > representing the 4 bytes that you wrote out earlier. > > Why not use peek4u or peek4s to read the data back out as these > are the 32 bit routines for accessing memory? > > Write 90: poke4( mem, 90 ) > Read 90: peek4u( mem ) > > Don > >
8. RE: Binary conversion
- Posted by Don Phillips <Graebel at hotmail.com> Aug 16, 2002
- 456 views
> Don, > > The problem was that the IBM iSeries stores 32 bit values in the > opposite order of the PC. What I wound up with after a call to a > program on the iSeries is 4 bytes returned to me in the opposite order. > > You can't use peek4s or peek4u on the memory since peek4s/u is expecting > > the bytes to be in the PC order. > > Make sense? Ya, complete. Thats just a pain in the rump =), looks like I will bow out of this thread. Source already posted on the topic looks efficient enough that I dont want to play with it heh. Don