473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to convert float to hex?

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

Nov 14 '05 #1
9 89922

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

Nov 14 '05 #2
FalkoG wrote:

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.
Nov 14 '05 #3
FalkoG wrote:
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
Nov 14 '05 #4
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
Nov 14 '05 #5
Thanks to all.
That looks great.

Regards
Falko

Nov 14 '05 #6
Chris Croughton <ch***@keristor.net> writes:
[...]
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>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
FalkoG 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.


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8
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....

Nov 14 '05 #9
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....

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Konrad Mühler | last post by:
Hi, a simple question but i found no solution: How can i convert a float value into a string value? string_value1 = string(float_value) + ' abc'
4
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here...
4
by: Pixie Songbook | last post by:
Hi, I'm trying to write a program using the Dev C++ 4.9.9.2 compiler that takes input numbers from a word document, sums them together, and then gives me a result. It should be easy as the book I...
7
by: ayaniv | last post by:
how to convert * WITHOUT USING ANY FUNCTION * a float number to binary? at least if someone can explain me how to do it it would be better then giving me the code itself.
3
by: flickimp | last post by:
Hi Got various fields in T-SQL where a decimal point and a zero follows each value: ie: 46.0 54.0 234.0
2
by: lisasahu | last post by:
how to convert float to string like there is atof atoi and itoa but is ther any method to convert float to string
5
by: sidhuasp | last post by:
Hi could anyone please tell me how to convert float value in my database ti HOUR/MIN format for my timesheet requirement. i have to do it in sql-reporting services
2
by: ireyes | last post by:
DEAR SIR, I SAW YOUR INTERNET QUESTION AND I HAVE THE SAME TROUBLE. CUOLD YOU HELP ME TO MAKE A FLOAT TO INTEGER CONVERTION? DO YOU HAVE ANY EXEL FILE THAT CAN DO THAT? REGARDS AND THANKS A LOT ...
9
by: tvnaidu | last post by:
I am doing floating point calculation inside C function, with debugger I can see float values in single step, if I want to print them on console, I am using printf, but printf not working for flaot,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.