Tom Freund-2 |
Have a 4-byte hex value representing a 32-bit floating point number in IEEE floating point format that is coming into a eLua chunk.
_______________________________________________ eLua-dev mailing list [hidden email] https://lists.berlios.de/mailman/listinfo/elua-dev |
Tom Freund-2 |
A little more explanation. The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it with "0x". I also tried using the base argument for tonumber (16) without using the "0x" prefix. Still, the resulting value is not correct.
Tom Freund "Systems overseeing public and private infrastructure" _______________________________________________ eLua-dev mailing list [hidden email] https://lists.berlios.de/mailman/listinfo/elua-dev |
Martin Guy |
On 27/05/2014, Tom Freund <[hidden email]> wrote:
> On Tue, May 27, 2014 at 2:33 PM, Tom Freund <[hidden email]> wrote: >> Have a 4-byte hex value representing a 32-bit floating point number in >> IEEE floating point format that is coming into a eLua chunk. >> >> Want to convert it back to a floating point value. Using tonumber does >> not >> yield the right value. Any suggestions ? > The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it > with "0x". I also tried using the base argument for tonumber (16) without > using the "0x" prefix. Still, the resulting value is not correct. I suspect you may need a C function to do the type conversion, using a union { float f; int i; }, storing your value in field i and reading it back from field f. I suspect that would be less painful than reconstructing the floating poinr value from the bitfields. Unless there's a better way... M M _______________________________________________ eLua-dev mailing list [hidden email] https://lists.berlios.de/mailman/listinfo/elua-dev |
Marcelo Politzer |
Maybe I missed the point but a string.format("%f", your_0x_number)
seems like it could work. Best, Marcelo 2014-05-27 16:44 GMT-03:00 Martin Guy <[hidden email]>: > On 27/05/2014, Tom Freund <[hidden email]> wrote: >> On Tue, May 27, 2014 at 2:33 PM, Tom Freund <[hidden email]> wrote: >>> Have a 4-byte hex value representing a 32-bit floating point number in >>> IEEE floating point format that is coming into a eLua chunk. >>> >>> Want to convert it back to a floating point value. Using tonumber does >>> not >>> yield the right value. Any suggestions ? > >> The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it >> with "0x". I also tried using the base argument for tonumber (16) without >> using the "0x" prefix. Still, the resulting value is not correct. > > I suspect you may need a C function to do the type conversion, using a > union { float f; int i; }, storing your value in field i and reading > it back from field f. I suspect that would be less painful than > reconstructing the floating poinr value from the bitfields. > > Unless there's a better way... > > M > > M > _______________________________________________ > eLua-dev mailing list > [hidden email] > https://lists.berlios.de/mailman/listinfo/elua-dev eLua-dev mailing list [hidden email] https://lists.berlios.de/mailman/listinfo/elua-dev |
In reply to this post by Martin Guy
On Tue, May 27, 2014 at 2:44 PM, Martin Guy <[hidden email]> wrote:
I was thinking one could also write a function to repack the hexadecimal (base 16) into a binary string maybe using something like this: and then use the pack/unpack functions:
Then depending on byte ordering: print(pack.unpack(("001a4e6a"):fromhex(), '>f')) 5 2.4158525654806e-39 print(pack.unpack(("001a4e6a"):fromhex(), '<f')) 5 6.2290375092292e+25
James Snyder
ph: (847) 448-0386 _______________________________________________ eLua-dev mailing list [hidden email] https://lists.berlios.de/mailman/listinfo/elua-dev |
Tom Freund-2 |
Found an alternative way of doing this. Thank you for all your suggestions. On Tue, May 27, 2014 at 4:14 PM, James Snyder <[hidden email]> wrote:
Tom Freund "Systems overseeing
public and private infrastructure" _______________________________________________ eLua-dev mailing list [hidden email] https://lists.berlios.de/mailman/listinfo/elua-dev |
Martin Guy |
On 28/05/2014, Tom Freund <[hidden email]> wrote:
> Found an alternative way of doing this. Of the suggestions to the specific problem you asked, James wins the shortest-and-most-elegant prize... what was your solution in the end? Avoiding having to do the conversion? Cheers M _______________________________________________ eLua-dev mailing list [hidden email] https://lists.berlios.de/mailman/listinfo/elua-dev |
Free forum by Nabble | Edit this page |