Bytes to floating point

Started by paul johansen, March 08, 2009, 06:14:06 AM

Previous topic - Next topic

paul johansen

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.

BudDurland

The PowerBasic CVS and MKI$ functions?

Bern Ertl

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