Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
greubel
Visitor

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 5
RokuMarkn
Visitor

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
Visitor

Re: roByteArray to float or double

IEEE float as eight bytes, 3F F0 00 00 00 00 00 00 = 1.0
0 Kudos
EnTerr
Roku Guru

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
Visitor

Re: roByteArray to float or double

I was hoping you would have a method !
0 Kudos
EnTerr
Roku Guru

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
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.