How to convert float to hex?

F

FalkoG

Hello colleague

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.
I would be pleased to get some help.

Regards
Falko
 
C

Chris Croughton

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
 
B

bstockwell

FalkoG said:
Hello colleague

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.
I would be pleased to get some help.

Regards
Falko

To convert the fractional part to base 16, use successive
multiplication by 16.
Suppose you want to convert say 0.12.
Take 0.12 x 16 ; this equals 1.92. The integer part, converted to hex,
becomes your next hex digit and the fractional part is again multiplied
by 16. Repeat this until the fractional part is zero, or you have
enough digits.
Thus
0.12 x 16 = 1.92 translation so far .1
..92 x 16 = 14.72 .1E
..72 x 16 = 11.52 .1EB
..52 x 16 = 8.32 .1EB8
..32 x 16 = 5.12 .1EB85
..12 x 16 = 1.92 so now it starts to repeat. So your
translated
number will be
.1EB851EB851EB85... in hexadecimal.
 
I

infobahn

FalkoG said:
Hello colleague

I want to convert a floating number for example 5236.9856982 to a
hexadecimal number.

Split the number into whole and fractional parts. modf can do this.

To convert the integer part, write down n % 16 as a hexadecimal
digit; n /= 16; until n is zero. Then reverse the string.

Now add a hexadecimal point, so to speak.

To convert the fractional part is not much different:

Loop through this:
f *= 16.0;
d = (int)f;
write down the hexadecimal digit corresponding to d.
f -= d;
until you have all the precision you can bring yourself to believe.
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.

Odd. It's not /that/ difficult.
 
M

Martin Ambuhl

FalkoG said:
Hello colleague

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
numerous
answer my question.
I would be pleased to get some help.

On the assumption that you are asking about representation for humans,
rather than being confused about internal forms,

#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <math.h>

int showhex(double x);
int showoct(double x);

int main(void)
{
double q = 5236.9856982;
/* check to see if your compiler supports the 'a' or 'A' specifier
with printf */
printf("The number represented as %.*g base 10 can be shown as\n",
DBL_DIG, q);
showhex(q);
printf("or\n");
showoct(q);
return 0;
}


/* The following showhex and showoct work over a restricted range of
values. If you need a larger range, handle the integer part
better. While doing that, generalize them to a single function
taking a base in the range 2-36 as in strtoul */

int showhex(double x)
{
double fracp, intp;
int y;

if (x < 0) {
x = -x;
putchar('-');
}
fracp = modf(x, &intp);
if (x > ULLONG_MAX) {
printf("(Number too large.\n");
return 1;
}
printf("%llx.", (unsigned long long) intp);
while (fracp) {
fracp *= 16;
y = fracp;
printf("%x", y);
fracp -= y;
}
printf(" hex\n");
return 0;
}


int showoct(double x)
{
double fracp, intp;
int y;

if (x < 0) {
x = -x;
putchar('-');
}
fracp = modf(x, &intp);
if (x > ULLONG_MAX) {
printf("(Number too large.\n");
return 1;
}
printf("%llo.", (unsigned long long) intp);
while (fracp) {
fracp *= 8;
y = fracp;
printf("%o", y);
fracp -= y;
}
printf(" octal\n");
return 0;
}

The number represented as 5236.9856982 base 10 can be shown as
1474.fc56b79cba hex
or
12164.7705326747135 octal
 
K

Keith Thompson

Chris Croughton said:
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.
[...]

<OT>
gcc is a compiler; it doesn't include a library. It uses whatever
library is provided on the underlying system (which may or may not be
the GNU glibc).
</OT>
 
C

CBFalconer

FalkoG said:
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.

Once you have separated the fractional part (i.e. 0.9856982) you
can express it as hex by:

static const char hex[] = "0123456789abcdef";
int i, n;
double r;

putchar('.');
r = 0.9856982; /* or whatever means you use */
for (i = 0; i < FRACT_DIGS_NEEDED; i++) {
n = (int) (r = r * 16.0);
r = r - n;
putchar(hex[n]);
}
putchar('\n');

UNTESTED code above. You can probably truncate trailing 0s by
making the continuation test be r != 0, but that may have weevils.
 
A

addy.amu

Hi Falko,
Such conversions u can easily find in any book pertaining to
Computer Architecture (eg. Computer System Architecture - Morris Mano)
Here i will try to explain ur query
say a number .12d to hexadecimal,

0.12 x 16 = 1.92 take interger part, 1
0.92 x 16 = 14.72 take integer part D
0.72 x 16 = 11.52 take integer part B
and so on

if u know C language i can provide u with C code to convert decimal
values to binary, octal and hexadecimal....
 
A

addy.amu

Hi Falko,
Such conversions u can easily find in any book pertaining to
Computer Architecture (eg. Computer System Architecture - Morris Mano)
Here i will try to explain ur query
say a number .12d to hexadecimal,

0.12 x 16 = 1.92 take interger part, 1
0.92 x 16 = 14.72 take integer part D
0.72 x 16 = 11.52 take integer part B
and so on.
Hence answer is 0.12d ~ 0.1DB....
in other words: 0.1DB = 1*(1/16) + 14*(1/256) + 11*(1/4056)+....=.12


if u know C language i can provide u with C code to convert decimal
values to binary, octal and hexadecimal....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top