On 5 Jan 2005 06:58:44 -0800, FalkoG
<ma***********@gmx.de> wrote:
I want to convert a floating number for example 5236.9856982 to a
hexadecimal number.
I'd tried several things but my problem is to convert the position
after decimal point. I searched numberless websites but nothing could
answer my question.
Well, the 'easy' way, if you have a C99 conforming library, is to use
sprintf with the %A qualifier. If you don't (the gcc one is still not
conforming in that way, I believe) you need to do it the long way.
Basically, having converted the part before the point, subtract that so
you are left with a number less than zero. Then for as many digits as
you want:
multiply by 16
convert the integer part to hex and add it to the buffer
get rid of the integer part
How many digits you want to convert is up to you.
You can use ldexp() to "multiply by 16" in a fast way.
(It so happens that I'm writing a printf replacement at the moment,
because I've become fed up with not having the C99 options available,
I'm just coming to the floating point part...)
Chris C