473,748 Members | 4,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 90417

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
.1EB851EB851EB8 5... 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.770532674 7135 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_Keit h) 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[] = "0123456789abcd ef";
int i, n;
double r;

putchar('.');
r = 0.9856982; /* or whatever means you use */
for (i = 0; i < FRACT_DIGS_NEED ED; 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********@yah oo.com) (cb********@wor ldnet.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
175415
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
23166
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 I was expecting 100.1234) When I do Select STR(Cast('100.1234' as float),25,4) I get back the result as 100.1234
4
25096
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 have tells me what to do, problem is I get the error message "cannot convert `float' to `float*' for argument `1' to `float array_sum(float*, int)' " for line 22 of the program. This is what I have; #include <iostream.h> #include <fstream.h> ...
7
46542
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
9787
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
6897
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
3294
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
817
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 IVAN REYES ___________________________________
9
5121
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, how can I convert them to int and print on console using printf. I tried to print with %d, but float, but not printing, any idea?. Thanks.
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.