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
|
Not Categorized, Please Help
|
|