PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: paul johansen on March 08, 2009, 06:14:06 AM

Title: Bytes to floating point
Post by: paul johansen on March 08, 2009, 06:14:06 AM
Hi!

I have a incoming UDP package that contains 4 Byte variables. I want to combine them into a single precision floating point value. howto?

I also need to convert a single precision floating point into 4 bytes.
Title: Re: Bytes to floating point
Post by: BudDurland on March 08, 2009, 11:02:49 AM
The PowerBasic CVS and MKI$ functions?
Title: Re: Bytes to floating point
Post by: Bern Ertl on March 08, 2009, 11:39:18 AM
How are you storing the 4 bytes?  in a string or byte array?  You can use a UNION TYPE or a pointer to recast the data.  For example.

UNION FourBytesOfFunType
  s AS STRING * 4
  a(1 TO 4) AS BYTE
  r AS SINGLE
END UNION

DIM FourBytesOfFun AS FourBytesOfFunType

receive data into FourBytesOfFun.s or FourBytesOfFun.a() and then process as FourBytesOfFun.r

or:


'Holds 4 bytes of data
LOCAL sData AS STRING
DIM bData( 1 TO 4) AS BYTE

LOCAL prData AS SINGLE PTR
LOCAL rData AS SINGLE

prData = STRPTR( sData)
or
prData = VARPTR( bData(1))

rData = @prData