Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
greubel
Level 7

roByteArray to float or double

Has anyone come up with a way to convert a 4 byte or 8 byte binary value in a roByteArray to a real floating point number ?
And visa versa, float or double to a 4 or 8 byte binary value.
0 Kudos
5 Replies
RokuMarkn
Level 7

Re: roByteArray to float or double

The 4 or 8 byte value in the ByteArray -- is this formatted as an IEEE floating point number? Or is it an integer?

--Mark
0 Kudos
greubel
Level 7

Re: roByteArray to float or double

IEEE float as eight bytes, 3F F0 00 00 00 00 00 00 = 1.0
0 Kudos
EnTerr
Level 8

Re: roByteArray to float or double

In a perfect world, roByteArray should get couple of pack/unpack methods:
function ifByteArray.unpack(fmt as String) as roArray
function ifByteArray.pack(fmt as String, data as roArray) as Boolean
where fmt = sequence of format characters:
fmt BrSc type  Size, octets
- --------- ------------
c String 1
b Integer 1
h Integer 2
i Integer 4
f Float 4 in IEEE 754
d Double 4 in IEEE 754
s String zero-terminated utf-8
? Boolean 1

I keep getting those urges to sketch a BSc function unpacking 4-byte float but not having a way to inject NaN, minus 0 and +/-Infinity values (luckily) keeps me from doing it.
0 Kudos
greubel
Level 7

Re: roByteArray to float or double

I was hoping you would have a method !
0 Kudos
EnTerr
Level 8

Re: roByteArray to float or double

Oh, "the things I'm gonna do for my country"*:

inf = 1e39
NaN = 0 * inf
negZero = -1 / inf

BrightScript Debugger> ? inf, NaN, negZero
inf nan -0
BrightScript Debugger> ? (negZero = 0), 0 * NaN, inf * NaN
true nan nan

(*) And curiousity, and vanity.
0 Kudos