1. conversion ??
How do I convert a Euphoria INTEGER to a machine-language 2-BYTE INTEGER ??
Thanks in advance BERNIE
2. Re: conversion ??
>>> Bernie Ryan <bwryan at PCOM.NET> 07/06/99 02:00PM >>>
How do I convert a Euphoria INTEGER to a machine-language 2-BYTE INTEGER =
??
Thanks in advance BERNIE
>>>>>>>>>>>>>>>>>>>>>>>>>
I'm not sure what you mean about converting. Euphoria uses 4-byte atoms, =
so as long as you're not using unsigned integers above 65,535 or signed =
integers outside plus or minus 32,767 you should be okay. Or to be extra =
safe, allocate a two-byte area in memory, then poke the integer (within =
the above ranges) into that area.
Michael J. Sabal
3. Re: conversion ??
I'am trying to pass an array of 16-bit integer and I am confused.
To use a EUPHORIA INTEGER in Machine language the INT_TO_BYTE function
is used for conversion to 32 bit machine langauge integers. There is no
conversion for 16-bit integers, and I want to be sure about the conversion.
Bernie
4. Re: conversion ??
>>> Bernie Ryan <bwryan at PCOM.NET> 07/06/99 03:11PM >>>
I'am trying to pass an array of 16-bit integer and I am confused.
To use a EUPHORIA INTEGER in Machine language the INT_TO_BYTE function
is used for conversion to 32 bit machine langauge integers. There is no
conversion for 16-bit integers, and I want to be sure about the conversion.=
Bernie
>>>>>>>>>>>>>>>>>>>>>>>
I'm too lazy to double check this right now, but perhaps this is what you =
want:
*** Untested code ***
integer myint
sequence a32, a16
myint =3D 24000
a32 =3D int_to_bytes(myint)
a16 =3D a32[3..4]
*********************
Since the two high bytes would be zeros anyway for an in-range integer, =
just taking the two low-bytes should work fine.
Michael J. Sabal
5. Re: conversion ??
Mike Sabal wrote:
>I'm too lazy to double check this right now, but perhaps this is what you
>want:
>
>*** Untested code ***
>
>integer myint
>sequence a32, a16
>
>myint = 24000
>a32 = int_to_bytes(myint)
>a16 = a32[3..4]
>
>*********************
int_to_bytes(24000) returns {192,93,0,0} -- so the last line should be:
a16 = a32[1..2]
Gabriel Boehme
6. Re: conversion ??
Bernie Ryan wrote:
> How do I convert a Euphoria INTEGER to a
> machine-language 2-BYTE INTEGER ??
I see I made an error in my example the other day. Here's an explanation of
the code, as well as a correction of the error. The code to allocate the
array is:
-- allot bytes for each element
address = allot( length( s ) * 2 )
Because each data element needs 2 bytes. As Michael Sabal pointed out, the
maximum value of a C integer is 65,535, which can be represented in 16 bits.
Since 1 byte = 8 bits, 16 bits = 2 bytes. So:
length( s ) = count of elements in array
count of elements * 2 bytes per element = space needed for integer array
Now you need to convert each Euphoria number into a C integer. The code:
-- convert number to bytes
bytes = int_to_bytes( s[i] )
converts the integer into 4 bytes. Because we are dealing with integers, the
most signifigant portion of the number (top 2 bytes) are going to be zero
anyway, so we can discard them; i.e.:
int_to_bytes( 1 ) --> {1,0,0,0}
int_to_bytes( 256 ) --> {0,1,0,0}
int_to_bytes( 54552 ) --> {40,252,0,0}
so to convert the number to a 2 byte C integer, you just take the least
signifigant bytes (first two). In the example code, I wrote:
-- poke word into memory
poke( pokeAt, bytes[1..size] )
Sorry about that; it should have been:
-- poke word into memory
poke( pokeAt, bytes[1..2] )
The code takes the two lowest bytes, and pokes them in the memory allocated
for the integer array. The next step of the code:
-- move ahead
pokeAt += 2
moves the memory pointer to the address allocated for the next integer
(remember, an integer takes up 2 bytes).
I hope it makes sense this time.
-- David Cuny
7. Re: conversion ??
- Posted by Bernie Ryan <bwryan at PCOM.NET>
Jul 06, 1999
-
Last edited Jul 07, 1999
Thanks everybody
I finally solved my problem. The reason I asked
was to be sure that I was using my code correctly because of an error
I was getting passing some parameters.
Bernie