![]() |
Quick & Dirty float to string converter
The following code sample will convert IEEE-754 32bit float/double as used in C18 to a string. There are some limitations, but it runs ok on my unix box. I haven't (and cannot at the moment) try this on PIC18F/RC. The hex binary test values were obtained by creating and populating a float variable under MPLAB and hand copying out the hex value for testing purposes. The unix box is native 64bit, so I may have messed up somewhere but thought I'd drop this 5-minute QnD code out for folks to look at/use if needed or wanted. I'm sure there is a better way, but its just Quick 'n Dirty so don't expect elegant/optimal/all-encompassing.
When run on unix it generates what looks like reasonable output: alpha> ./printff 1.234567879 1.233999963 1234.000000000 1234567936.000000000 0.123399970 0.001233931 0.000012276 Should work with printff( (long)float-variable-name ). Bud PS Derived by reading http://en.wikipedia.org/wiki/IEEE_754 Code:
#include <stdio.h> |
Re: Quick & Dirty float to string converter
nice job man, im sure it would be useful.
|
Re: Quick & Dirty float to string converter
Fixed a couple bugs.
Test cases run: 1.234567879 1.233999963 1234.000000000 1234567936.000000000 0.123399970 0.001233990 0.000012335 0.156250000 98.765434262 9.876543043 0.987654324 Code:
void printff( long fbinary ) |
| All times are GMT -5. The time now is 00:24. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi